日期:2014-05-19  浏览次数:21071 次

如何用DataSet和Adapter向access中添加记录
string   strPath   =   "E:\\weimin\\KaoQin.mdb ";
OleDbConnection   myConn   =   new   OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;   Data   Source   =   "   +   strPath);
myConn.Open();

string   strSQL   =   "SELECT   *   FROM   KAOQIN ";
OleDbDataAdapter   myODA   =   new   OleDbDataAdapter(strSQL,   myConn);

DataSet   myDS   =   new   DataSet();
myODA.FillSchema(myDS,   SchemaType.Source,   "KAOQIN ");
myODA.Fill(myDS,   "KAOQIN ");

DataRow   myRow   =   myDS.Tables[ "KAOQIN "].NewRow();

myRow[ "ROWID "]   =   2;
myRow[ "USERID "]   =   99;
myRow[ "DATE "]   =   "2007-07-03 ";
myRow[ "SHANGBANTIME "]   =   "2007-07-03   19:00:00 ";
myRow[ "XIABANTIME "]   =   "2007-7-03   19:00:00 ";
myRow[ "GONGSHI "]   =   0;
myRow[ "QIANQIN "]   =   0;
myRow[ "JIABAN "]   =   0;

myDS.Tables[ "KAOQIN "].Rows.Add(myRow);

OleDbCommandBuilder   myCommandBuilder   =   new   OleDbCommandBuilder(myODA);

myODA.Update(myDS,   "KAOQIN ");

为什么我用上面的方法添加记录,在update语句总是提示insert   sql语句有问题。我是照微软的例子来的呀。各位,请赐教。

------解决方案--------------------
一)给数据库设置主键
二)不要用Date,UserID,RowID这样的词给列命名
------解决方案--------------------
Date是Access的关键字,建议你把数据库相应的字段名改名(MyDate),然后把myRow[ "DATE "] = "2007-07-03 ";改为 myRow[ "MyDATE "] = "2007-07-03 ";就ok了

------解决方案--------------------
date是Access的数据类型 就是 日期/时间 类型