用Excel做DB,连接字符串如下:
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEST.xls;Extended Properties='Excel 8.0;HDR=YES;'";
用OleDbDataAdapter对Excel进行数据插入,代码如下:
OleDbConnection oleConn = new OleDbConnection(ConnectionString);
oleConn.Open();
OleDbCommand oleCmd = new OleDbCommand("select * from " + tableName, oleConn);
OleDbDataAdapter da = new OleDbDataAdapter(oleCmd);
ds1 = new DataSet();
da.Fill(ds1, "[sheet1$]");
ds1.Tables[tableName].PrimaryKey = new DataColumn[] { ds1.Tables[tableName].Columns[0] };
DataRow row = ds1.Tables["[sheet1$]"].NewRow();
row[0] = 1;
row[1] = "Hello";
OleDbCommandBuilder myCommandBuilder = new OleDbCommandBuilder(da);
da.InsertCommand = myCommandBuilder.GetInsertCommand();
da.Update(ds1,tableName); //提示“Syntax error in INSERT INTO statement”
请教大侠,问题到底出在哪里,郁闷了许久,google了也没找出问题。
ps:也设了主键,应该不是主键问题
在Excel中试把这个"sheet1"改名。
ssssssssssssss