"此SqlTransaction已完成;它再也无法使用。" 是什莫原因? SqlTransaction 使用了Commit()后就是不能在使用了,必须重新BeginTransaction()一个。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的SqlTransaction 已经Commit或者RollBack之后,你又使用SqlTransaction所在的Connection去做Select、Insert、 Delete,就会出现这个错误。 不是呀,Commit之后,断开了Connection,也重新BeginTransaction() open(); string strSql=" ..."; SqlTransaction myTransaction=sqlConnection.BeginTransaction(); try { SqlCommand myCommand=sqlConnection.CreateCommand(); myCommand.Connection=sqlConnection; myCommand.Transaction=myTransaction; myCommand.CommandText=strSql; SqlDataAdapter myAdapter=new SqlDataAdapter(); myAdapter.SelectCommand=myCommand; SqlCommandBuilder myBuilder=new SqlCommandBuilder(myAdapter); myAdapter.FillSchema(TrafficDT,SchemaType.Mapped); .... Count=myAdapter.Update(TrafficDT); myTransaction.Commit(); } catch(Exception ex) { throw ex; } finally { close(); } Commit之后,没有RollBack;try{ ... trans.Commit();}catch(Exception err){ trans.RollBack(); throw err; }//没有RollBack ,如何确定你的trans有效呢? 把open和BeginTransaction放到try里头去,并在catch里头添加回滚处理(如果事务开启)。上面的方法或许对你的问题没帮助,但应该这么做。 试试try{ SqlTransaction Trs; Trs = sqlConnection.BeginTransaction(); try { DataAdapter1.Update(ds,"dt"); Trs.Commit(); } catch(System.Exception e) { if(Trs != null) { Trs.Rollback(); } throw new Exception(e.Message); } finally { if(Trs != null) { Trs.Dispose(); } } }catch(System.Exception e){ throw new Exception(e.Message);} myTransaction.Rollback();是写在catch里边了,是我刚才没贴全! 试试不写Open(),不知会不会自动Open //你的代码在有异常的情况下出现问题.Count=myAdapter.Update(TrafficDT);myTransaction.Commit();}catch(Exception ex){ myTransaction.Rollback();//throw ex;} DirectX获取声卡数据无法正常播放问题 C#.NET插件式开发 三表联合查询问题 在 System.ArgumentOutOfRangeException 中第一次偶然出现的“System.Windows.Forms.dll”类型的异常 WebService的问题! 求一字符串中的第一个数字的索引。。。。 C#如何实现定时任务 高手帮我看看这个问题,急啊!~~~~ 5000分找Bug之二 如何限制窗口出现滚动条。 请问一个关于SqlXmlCommand的问题 谁给我个实现搜索功能的代码
open();
string strSql=" ...";
SqlTransaction myTransaction=sqlConnection.BeginTransaction(); try
{
SqlCommand myCommand=sqlConnection.CreateCommand();
myCommand.Connection=sqlConnection;
myCommand.Transaction=myTransaction;
myCommand.CommandText=strSql; SqlDataAdapter myAdapter=new SqlDataAdapter();
myAdapter.SelectCommand=myCommand;
SqlCommandBuilder myBuilder=new SqlCommandBuilder(myAdapter);
myAdapter.FillSchema(TrafficDT,SchemaType.Mapped);
....
Count=myAdapter.Update(TrafficDT);
myTransaction.Commit();
}
catch(Exception ex)
{
throw ex;
}
finally
{
close();
}
...
trans.Commit();
}
catch(Exception err){
trans.RollBack();
throw err;
}//没有RollBack ,如何确定你的trans有效呢?
上面的方法或许对你的问题没帮助,但应该这么做。
try
{
SqlTransaction Trs;
Trs = sqlConnection.BeginTransaction();
try
{
DataAdapter1.Update(ds,"dt");
Trs.Commit();
}
catch(System.Exception e)
{
if(Trs != null)
{
Trs.Rollback();
}
throw new Exception(e.Message);
}
finally
{
if(Trs != null)
{
Trs.Dispose();
}
}
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
是写在catch里边了,是我刚才没贴全!
myTransaction.Commit();
}
catch(Exception ex)
{
myTransaction.Rollback();//
throw ex;
}