可能重复:
ExecuteNonQuery requires the command to have a transaction error in my code
strSQL = "insert into............";
SqlTransaction objSqlTransaction = Master.objSqlDbComm.SqlConnectionObject.BeginTransaction();
try
{
Master.objSqlDbComm.ExecuteNonQuery(strSQL);
objSqlTransaction.Commit();
}
catch(Exception)
{
objSqlTransaction.Rollback();
}
finally
{
objSqlTransaction.Dispose();
}
当我使用上面的代码获取错误
时当分配给命令的连接处于挂起的本地事务中时,ExecuteNonQuery要求命令具有事务。该命令的Transaction属性尚未初始化。
答案 0 :(得分:1)
您需要将命令对象提供给ExecuteNonQuery
做这样的事情:
SqlCommand cmd = new SqlCommand(your_sqlText, your_sqlcon, your_sqlTrans);
cmd.ExecuteNonQuery();