我用如下方法为什么没有把记录写到表里?
private void button2_Click(object sender, System.EventArgs e)
{
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Temp\\Money\\db1.mdb";
string strSQL = "SELECT * FROM tbl1" ;
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,myConn);
DataSet ds = new DataSet();
da.Fill(ds);
DataRow row = ds.Tables[0].NewRow();
row["id"] = 2 ;
row["Name"] = "wilson";
ds.Tables[0].Rows.Add(row);
ds.AcceptChanges();
da.Update(ds);
MessageBox.Show("OK");
}
8 个解决方案
要用如下的参考一下:
最后把结果写回数据库:
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{
return false;
}
finally
{
conn.Close();
}
在
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,myConn);
后加上
OleDbCommandBuilder custCB = new OleDbCommandBuilder(da);
不过,你所操作的表必须有主键。
修改如下:
.......
DataSet ds = new DataSet();
da.Fill(ds);
DataRow row = ds.Tables[0].NewRow();
row["id"] = 2 ;
row["Name"] = "wilson";
ds.Tables[0].Rows.Add(row);
da.Update(ds);
ds.AcceptChanges();
....
同意Knight94(愚翁)
使用oleDbCommandBuilder,这里有个例子,帮助里:
ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemdataoledboledbcommandbuilderclasstopic.htm
你也可以在帮助里看oleDbCommandBuilder类的详细说明。
当然会出错了,因为你开始先调用ds.AcceptChanges();会将行状态改为unchanged,再调用da.Update(ds);时没有改变状态的行,它不会查找相应的sql语句,所以没有报错,也没有更新数据库。
按照浪子兄说的修改以后,代码是正确的,可是因为你的update没有设置相应的insertcommand语句,所以会报错。
基本就这两个错误
da.Update(ds);
ds.AcceptChanges();
楼上老兄说的是啊