先看代碼?
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
提交
取消