110,534
社区成员
发帖
与我相关
我的任务
分享
public static bool UpdateDataBase(DataSet ds, string[] tableName, string[] tempSql)
{
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
SqlTransaction tran = conn.BeginTransaction();
try
{
for (int i = 0; i < tableName.Length; i++)
{
SqlCommand cmd = new SqlCommand(tempSql[i], conn);
cmd.Transaction = tran;
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder bld = new SqlCommandBuilder(da);
bld.ConflictOption = ConflictOption.OverwriteChanges;
for (int j = 0; j < bld.GetInsertCommand().Parameters.Count; j++)
{
bld.GetInsertCommand().Parameters[j].IsNullable = true;
}
for (int j = 0; j < bld.GetUpdateCommand().Parameters.Count; j++)
{
bld.GetUpdateCommand().Parameters[j].IsNullable = true;
}
da.Update(ds, tableName[i]);
}
ds.AcceptChanges();
tran.Commit();
return true;
}
catch
{
tran.Rollback();
throw;
}
}