如何将DataTable更新到数据库中
adapter.Update(dt)这样更新老是提示未将对象引用。。。。
不知adapter还要设置什么属性才可以使用这个方法来更新?谢谢
------解决方案--------------------代码贴出来
------解决方案--------------------未将对象引用:对象为null
------解决方案--------------------1 conn.open()
2 检查connString 格式:Connection=Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=DBName;Data Source=(local)
UserID=sa
Password=XXXX
CommandTimeout=90
------解决方案--------------------在给你个例子,是MSDN上的
public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName)
{
SqlConnection myConn = new SqlConnection(myConnection);
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
myConn.Open();
DataSet ds = new DataSet();
myDataAdapter.Fill(ds, myTableName);
//code to modify data in DataSet here
//Without the SqlCommandBuilder this line would fail
myDataAdapter.Update(ds, myTableName);
myConn.Close();
return ds;
}
------解决方案--------------------dt
先前没有释放吧
------解决方案--------------------错误是说你的表是空的,是不是在传datatable时,表没有实例化?
------解决方案--------------------if (null == adp) return null;
OracleCommandBuilder build = new OracleCommandBuilder((OracleDataAdapter)adp);
if (null != build)
{
try
{
adp.InsertCommand = build.GetInsertCommand(true);
adp.UpdateCommand = build.GetUpdateCommand(true);
adp.DeleteCommand = build.GetDeleteCommand(true);
}
catch (Exception ex)
{
}
}
//你的adapter的InsertCommand/UpdateCommand 都没有赋值,是NULL
------解决方案--------------------我看是,你的数据库中,要有一个主键,不然,就会错误,我遇到过
------解决方案--------------------真的是这个原因吗?呵呵
------解决方案--------------------up!