public  static  int  insert(Person  p)  
            {  
             OleDbConnection  con=DB.createConnection();  
             con.Open();  
             OleDbCommand  cmd  =  new  OleDbCommand("insert  into  person(pID,pName,pSex)  values(p.pID  ,  p.pName  ,  p.pSex)",con);  
             return(cmd.ExecuteNonQuery());  
。  
使用access数据库,写入权限也对  
 
报错如下:  
INSERT  INTO  语句的语法错误。    
说明:  执行当前  Web  请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。    
 
异常详细信息:  System.Data.OleDb.OleDbException:  INSERT  INTO  语句的语法错误。  
 
源错误:    
 
 
行  40:                                                  con.Open();  
行  41:                                                  OleDbCommand  cmd  =  new  OleDbCommand("insert  into  person(pID,pName,pSex)  values(p.pID  ,  p.pName  ,  p.pSex",con);  
行  42:                                                  return(cmd.ExecuteNonQuery());  
行  43:  //                                                if(count>0)  
行  44:  //                                                {  
   
 
源文件:  d:\workspace\test\adonet\dboperator.cs        行:  42    
 
堆栈跟踪:    
 
 
[OleDbException  (0x80040e14):  INSERT  INTO  语句的语法错误。]  
     System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32  hr)  
     System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS  dbParams,  Object&  executeResult)  
     System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&  executeResult)  
     System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior  behavior,  Object&  executeResult)  
     System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior  behavior,  String  method)  
     System.Data.OleDb.OleDbCommand.ExecuteNonQuery()  
     AdoNet.DBOperator.insert(Person  p)  in  d:\workspace\test\adonet\dboperator.cs:42  
     AdoNet.WebForm1.Button1_Click(Object  sender,  EventArgs  e)  in  d:\workspace\test\adonet\webform1.aspx.cs:87  
     System.Web.UI.WebControls.Button.OnClick(EventArgs  e)  
     System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String  eventArgument)  
     System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler  sourceControl,  String  eventArgument)  
     System.Web.UI.Page.RaisePostBackEvent(NameValueCollection  postData)  
     System.Web.UI.Page.ProcessRequestMain()  
 
   
请问该怎么写?

解决方案 »

  1.   

    如果你的name,sex是字符类型,那么就应该这样写:
    OleDbCommand("insert  into  person(pID,pName,pSex)  values(p.pID  , '+p.pName+'  ,  '+p.pSex+'",con);
      

  2.   

    如果你的name,sex是字符类型OleDbCommand  cmd  =  new  OleDbCommand("insert  into  person(pID,pName,pSex)  values('"+p.pID+"'  ,  '"+p.pName+"'  ,  '"+p.pSex+"')",con);
      

  3.   

    xray2005(我是“风车车”,因为人要活的像“风车车”!才快乐!!什么是风车车啊?来四川就知道了!) 
    给的答案我知道什么意思,不过好像写错咯
    所以各给10算啦
      

  4.   

    这个是正解 OleDbCommand  cmd  =  new  OleDbCommand("insert  into  person(pID,pName,pSex)  values('"+p.pID+"'  ,  '"+p.pName+"'  ,  '"+p.pSex+"')",con);
      

  5.   

    原来我用的是&
    在asp.net里不能用&吗