使用SQLcommandBuilder的C#批量更新异常 [英] C# Bulk Update Exception using SQLcommandBuilder
问题描述
我正在尝试使用sqlcommandBuilder更新/插入,但是在21st更新20条记录后却遇到问题,这是一些sql异常(可能是数据类型,值的范围...)21st记录由于某些sql异常而没有更新.因为所有其他记录都将回滚.请指导我解决此问题.
我想如果异常会出现,那么如何处理而又不影响事务回滚.
任何异常x,y,z.在异常发生之前,我想要我应该在数据库中保留的插入/更新的内容.如何处理此问题,请给我指导
从评论中复制:
Hi,
I was trying to update/Insert with sqlcommandBuilder but was getting problem after updating 20 record in 21st there was some sql exception(may be data type,range of value.....) the 21st record was not updating because of some sql exception.because of this all other record was getting rollback.Please guide me to solve this problem.
I am supposing if exception will come then how to handle with out effecting the transaction to rollback.
Any exception x,y,z.I want before exception what ever i inserted/Updated that should persist in data base How to handle this Please Guide me guys
Copied from a comment:
string TableName = "EmpDat";
DataTable orgTable = new DataTable();
orgTable.TableName=TableName;
orgTable.Columns.Add("EmpName", typeof(string));
orgTable.Columns.Add("Desig", typeof(string));
orgTable.Columns.Add("EmpFname", typeof(string));
orgTable.Columns.Add("Salary", typeof(int));
orgTable.Columns.Add("Sex", typeof(string));
orgTable.Columns.Add("Branch", typeof(string));
DataRow dr = orgTable.NewRow();
dr["EmpName"] = "Jack";
dr["Desig"] = "Chawki Daar";
dr["EmpFname"] = "Khain The";
dr["Salary"] = 12;
dr["Sex"] = "H";
dr["Branch"] = "Not Allowed";
orgTable.Rows.Add(dr);
SqlTransaction tran;
SqlConnection con = new SqlConnection("Constr");
con.Open();
tran = con.BeginTransaction();
SqlCommand cmd = new SqlCommand("select * from EmpDetail", con);
cmd.Transaction = tran;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"EmpDat");
DataRow[] dr1 = ds.Tables[0].Select("empname='Jack'");
dr1[0]["empName"] = "Good Morning";
orgTable.AcceptChanges();
DataTable f = orgTable.GetChanges(DataRowState.Modified);
// con.Open();
SqlCommandBuilder sqlcmdBuild = new SqlCommandBuilder(da);
da.Update(ds.Tables[0]);
推荐答案
尝试使用Try catch,然后在catch块中提交SqlTransaction
像
Try using Try catch and then commit your SqlTransaction in catch block
something like
try
{
//Your code here;
}
Catch(Exeception ex)
{
//commit your SqlTransaction here;
}
这篇关于使用SQLcommandBuilder的C#批量更新异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!