之前我写好了一个数据访问层(oledb),可以用来操作oracle和sqlServer,都没问题.可是我昨天用它来操作Access数据库就有问题了.查询Access数据库是没有问题的,只是我想用adapter1.Update(updateDataSet, tableName),老是提醒我说是insert into的语法有错误,怎么可能呢!我先前用它操作oracle和sqlServer是没有问题的,而且我也跟踪了一下insert into语句的具体格式,是没有问题的!!请高手解答.....在线等!               非常感谢

解决方案 »

  1.   

    如果是Access在插入日期类型时,如果insert into tablename (date1) values ("2006-09-05")就会发生异常,将引号改为#试试
      

  2.   

    在access里有些字段名字为ID之类的列不能正确查的,最好加上表名
      

  3.   

    我全部都加了表名,可是还是不能查,
    如果写Select * from table where 1=2 是可以查询的,
    可是写成select field1,field2 from table where 1=2 就有问题了
      

  4.   

    takemoto(竹本) :我是用的commandBuilder自动配置的sql语句,怎么改呀!
      

  5.   

    我所用的sql语句是Access查询器提供的,能够执行,没问题.
    现在问题找到了.是我要插入的字段中有日期类型,可我不知道怎么改?
      

  6.   

    takemoto(竹本) :能帮我解决一下吗?
      

  7.   

    to 现在问题找到了.是我要插入的字段中有日期类型,可我不知道怎么改?"insert into yourTable(yourDateTime) values("
       +"'"+DateTime.Now.Date+"'"日期如上保存试试看。
      

  8.   

    我的insert into 语句是由commandbuilder自动配置的!!请大家看一下我的代码!!!
    //cmdBuilder.RefreshSchema();
    OleDbCommand insertCommand = cmdBuilder.GetInsertCommand();
    OleDbCommand updateCommand = cmdBuilder.GetUpdateCommand();
    OleDbCommand deleteCommand = cmdBuilder.GetDeleteCommand(); OleDbCommand insertCommand1 = new OleDbCommand();
    OleDbCommand updateCommand1 = new OleDbCommand();
    OleDbCommand deleteCommand1 = new OleDbCommand(); int columnCount = ds.Tables[tableName].Columns.Count; string insertText = insertCommand.CommandText; string str1 = updateCommand.CommandText;
    string updateText = "";
    int indexOfWHERE1 = str1.IndexOf(" WHERE ");
    int indexOfOkAND1 = str1.IndexOf(" AND ",indexOfWHERE1);
    if (indexOfOkAND1 >-1)
    updateText = str1.Substring(0, indexOfOkAND1) + " )";
    else
    updateText = str1;
    //string updateText = str1.Substring(0, indexOfOkAND1) + " )"; string str2 = deleteCommand.CommandText;
    // int indexOfWHERE2 = str2.IndexOf(" WHERE ");
    // int indexOfOkAND2 = str2.IndexOf(" AND ",indexOfWHERE2);
    // string deleteText = str2.Substring(0, indexOfOkAND2) + " )";
    string deleteText;
    int indexOfWHERE2 = str2.IndexOf(" WHERE ");
    int indexOfOkAND2 = str2.IndexOf(" AND ",indexOfWHERE2);
    if (indexOfOkAND2 > -1)
    deleteText = str2.Substring(0, indexOfOkAND2) + " )";
    else
    deleteText = str2;
    //string deleteText = str2; insertCommand1.CommandText= insertText;
    updateCommand1.CommandText= updateText;
    deleteCommand1.CommandText= deleteText; insertCommand1.Connection = insertCommand.Connection;
    updateCommand1.Connection = insertCommand.Connection;
    deleteCommand1.Connection = insertCommand.Connection; foreach(OleDbParameter parm in insertCommand.Parameters)
    {
    insertCommand1.Parameters.Add( (OleDbParameter)((ICloneable)parm).Clone() );
    } for(int i=0; i<=columnCount; i++)
    {
    updateCommand1.Parameters.Add( (OleDbParameter)((ICloneable)updateCommand.Parameters[i]).Clone() ); } deleteCommand1.Parameters.Add( (OleDbParameter)((ICloneable)deleteCommand.Parameters[0]).Clone() ); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.DeleteCommand = deleteCommand1;
    adapter.InsertCommand = insertCommand1;
    adapter.UpdateCommand = updateCommand1; if(deleteCommand.Connection.State != ConnectionState.Open)
    deleteCommand.Connection.Open(); return adapter.Update(ds, tableName);
      

  9.   

    用参数化的方式赋值就,参考:
    string strConn = "...";
    string strCmd = "insert into yourtable(cl1, cl2, cl3 ) values(?,?,?)"; OledbConnection conn = new OledbConnection( strConn );
    OledbCommand comm = new OledbCommand( strCmd, conn );comm.Parameters.Add( "@cl1", DateTime.Now.ToString() );
    comm.Parameters.Add( "@cl2", 插入的值 );
    comm.Parameters.Add( "@cl3", 插入的值 );conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
      

  10.   

    在ACCESE中,日期采用"#"号标志,不能采用"'"
      

  11.   

    是啊,日期格式,我是这样写的,可是老是提示insert into 语法错误!!我不知道怎么解决
      

  12.   

    insert into ZhengJ(Pass_num,Per_num,Type,Iss_date,Exp_date,Status,iss_offi,Note,Chg_date,Place,State_date,State) 
    values('D000000043', '4400000000002356', '护照', #2006-8-25 10:38:23#,#2008-8-25 0:00:00#, '已发证', '', '', #2006-8-26 9:51:28#, '', '', '')请大家帮我检查一下
      

  13.   


    insert into ZhengJ(Pass_num,Per_num,Type,Iss_date,Exp_date,Status,iss_offi,Note,Chg_date,Place,State_date,State) 
    values('D000000043', '4400000000002356', '护照', '#2006-8-25 10:38:23#','#2008-8-25 0:00:00#', '已发证', '', '', '#2006-8-26 9:51:28#', '', '', '')试试看
      

  14.   

    还是不行呀!!我真的头都大了,一直做SqlServer和Oracle,没想今天被Access给难倒了,兄弟们,快出招呀!
      

  15.   

    adapter1.Update(updateDataSet, tableName)
    要看是什么类型的adapter,要看是什么类型的数据库
    对不同的数据库,最好自己写不同的sql语句
    在sql server能支持的语句,有部分是Access不支持的
      

  16.   

    你的有些字段可能是Access的保留字,不能用的!!
      

  17.   

    最基本的,字段Note在sql server可以,但在Access下不行滴
      

  18.   

    列举一些常见的保留字:User、Password、Key 等等,呵呵,这些可不能用,用了直接让你晕倒
      

  19.   

    插入时将字段名用[]起来;如insert into table1 ([id],[name]) values(1,'test1')
    修改时在字段前加上表名;如update table1 set table1.name='test2' where id=1
      

  20.   

    insert into ZhengJ(Pass_num,Per_num,Type,Iss_date,Exp_date,Status,iss_offi,Note,Chg_date,Place,State_date,State) 
    values('D000000043', '4400000000002356', '护照', #2006-8-25 10:38:23#,#2008-8-25 0:00:00#, '已发证', '', '', #2006-8-26 9:51:28#, '', '', '')改为insert into ZhengJ([Pass_num],[Per_num],[Type],[Iss_date],[Exp_date],[Status],[iss_offi],[Note],[Chg_date],[Place],[State_date],[State]) 
    values('D000000043', '4400000000002356', '护照', #2006-8-25 10:38:23#,#2008-8-25 0:00:00#, '已发证', '', '', #2006-8-26 9:51:28#, '', '', '')