下面是一个简单的例程,用于测试有错误时事务的回滚。VB.NET 告诉我 Catch 下的变量 tr.Rollback() “变量 tr.Rollback() 在被赋值之前已被使用”以及公共覆盖。
由于这是在 Try/Catch/End Try 中使用的,我原以为它会按照 Try 中的分配进行分配。
该程序确实按预期工作。
对此的任何帮助将不胜感激。
MySQL 5.1.36 版和使用 InnoDB 的表
选项严格开启
导入 MySql.Data.MySqlClient 模块 Module1
Sub Main()
Dim cs As String = "Database=testdb;Data Source=qn-server1;" _
& "User Id= xxxxxx"
Dim conn As New MySqlConnection(cs)
Dim cmd As New MySqlCommand()
Dim tr As MySqlTransaction
Try
conn.Open()
tr = conn.BeginTransaction()
cmd.Connection = conn
cmd.Transaction = tr
cmd.CommandText = "UPDATE Authors SET Name = 'Leo Tolstoy' WHERE Id = 1"
cmd.ExecuteNonQuery()
cmd.CommandText = "UPDATE Books SET Title = 'War and Peace' WHERE Id = 1"
cmd.ExecuteNonQuery()
cmd.CommandText = "UPDATE Books SET Titl = 'Anna Karenina' WHERE Id = 2" <----- DELIBERATE ERROR on "Titl" TO TEST THE ROLL BACK I.E tr.Rollback()
cmd.ExecuteNonQuery()
tr.Commit()
conn.Close()
Catch ex As MySqlException
tr.Rollback() <------------------------------ THIS LINE "Variable "tr" has been assigned a value.............
Console.WriteLine("Error: " & ex.ToString())
End Try
Console.ReadLine()
End Sub
端模块