我正在使用SqlTransaction
将数据从DataTable更新到SQL Server表。
代码运行良好,没有任何错误。但是由于某种原因,更改未反映在表格中。没有遇到任何错误。
下面是我的C#代码(不是完整的代码)
using (con)
{
if (con.State == ConnectionState.Closed || con.State == ConnectionState.Broken)
con.Open();
using (SqlTransaction trans = con.BeginTransaction())
{
using (SqlCommand cmd = new SqlCommand(strCommand, con, trans))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
da.InsertCommand = cmb.GetInsertCommand();
da.UpdateCommand = cmb.GetUpdateCommand();
// da.DeleteCommand = cmb.GetDeleteCommand();
da.InsertCommand.Transaction = trans;
da.UpdateCommand.Transaction = trans;
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(sourceDt);
if (sb.Length > 0)
{
trans.Rollback();
throw new Exception(sb.ToString());
}
sourceDt.Merge(clonedDt, false, MissingSchemaAction.AddWithKey);
try
{
noOfRowsAffected = da.Update(sourceDt);
trans.Commit(); // Commit the changes to database
}
catch (Exception ex)
{ }
if (noOfRowsAffected > 0)
{
Boolean success = true;
}
}
}
}
}
如果我缺少任何内容,请标记更改。