我正在使用批处理更新来更新sql server 2005数据库,如下所示
cmd = new SqlCommand("update Table1 set column1 = @column1 where EmpNo = @EmpNo", con);
cmd.Parameters.Add(new SqlParameter("@column1", SqlDbType.VarChar));
cmd.Parameters["@column1"].SourceVersion = DataRowVersion.Current;
cmd.Parameters["@column1"].SourceColumn = "Column";
cmd.Parameters.Add(new SqlParameter("@EmpNo", SqlDbType.Int));
cmd.Parameters["@EmpNo"].SourceVersion = DataRowVersion.Current;
cmd.Parameters["@EmpNo"].SourceColumn = "EmpNo";
cmd.UpdatedRowSource = UpdateRowSource.None;
sqlDa = new SqlDataAdapter();
con.Open();
sqlDa.UpdateCommand =cmd;
sqlDa.UpdateBatchSize = 10;
sqlDa.Update(dt);
con.Close();但是数据没有更新。我不知道什么是problem.Any帮助。
发布于 2009-03-04 14:32:07
我建议您在发出update命令之前查看dt。确保有一些行的RowState为“更新”或“已添加”。如果没有,那么(我假设)您的DataTable中没有任何东西可以更新到数据库中。
另外,尝试删除.SourceVersion属性集操作。
如果一切正常,那么就在发出.Update之前开始对数据库进行跟踪。
这些只是尝试的几个第一步。
发布于 2012-03-19 21:12:34
SqlDataAdapter方法
使用(SqlCommand insertCommand=new SqlCommand(
“插入BulkLoadTable(FieldA,FieldB)值(@FieldA,@FieldB)",连接)
{ insertCommand.Parameters.Add("@FieldA",SqlDbType.VarChar,10,"FieldA");
insertCommand.Parameters.Add("@FieldB", SqlDbType.Int, 4, "FieldB");
// Setting UpdatedRowSource is important if you want to batch up the inserts
insertCommand.UpdatedRowSource = UpdateRowSource.None;
using (SqlDataAdapter insertAdapter = new SqlDataAdapter())
{
insertAdapter.InsertCommand = insertCommand;
// How many records to send to the database in one go (all of them)
insertAdapter.UpdateBatchSize = myDataTable.Rows.Count;
// Send the inserts to the database
insertAdapter.Update(myDataTable);
}}
https://stackoverflow.com/questions/610505
复制相似问题