先看代码
string connectionString = "Data Source=localhost;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=123456"; SqlConnection sqlConnection = new SqlConnection(connectionString); string strSQL = " insert into test1 values('1','a') "; SqlCommand command = new SqlCommand(strSQL, sqlConnection); sqlConnection.Open(); command.Transaction = command.Connection.BeginTransaction(); try { command.ExecuteNonQuery(); command.Transaction.Commit(); } catch { command.Transaction.Rollback(); } finally { sqlConnection.Close(); }
其中SQL语句中的第二个字段插入了a,但是数据库是数字型的。造成command.ExecuteNonQuery()出错,进入catch块Rollback,这个时候我发现在catch中command.Transaction变成了null。使得我Rollback要出异常。但是我把第二个字段改成数字以后,但是长度超过我数据库的长度,发现Transaction就不会丢,能正常Rollback。求高手解答!
添加回答
举报
0/150
提交
取消