代码如下:
Using Trans As SqlTransaction = con.BeginTransaction()
Dim cmd As SqlCommand = con.CreateCommand
cmd.Transaction = Trans
For I As Integer = 0 To 1
cmd.CommandText = "create table TT(F1 int,F2 int)"
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
End Try
Next
Trans.Commit() ‘运行到此,错误提示:此 SqlTransaction 已完成;它再也无法使用
End Using
请问这是为什么?怎么解决呢?
------解决方案--------------------
Transaction开始了吗?
------解决方案--------------------
Using Trans As SqlTransaction = con.BeginTransaction() 这句话就表明已经开始了,
------解决方案--------------------
1 确认con已经打开
2 确认SqlCommand 是否创建成功
3 对con的状态那一块多注意一下 看看 con是否过早的关闭了?
------解决方案--------------------
调试的时候看con和trans变量的值。看看他什么时候关的。
------解决方案--------------------
For I As Integer = 0 To 1
cmd.CommandText = "create table TT(F1 int,F2 int)"
Try
cmd.ExecuteNonQuery()
如果把上面换成
cmd.CommandText = "create table TT(F1 int,F2 int)"
cmd.ExecuteNonQuery()
cmd.CommandText = "create table TT2(F1 int,F2 int)"
cmd.ExecuteNonQuery()
这样是不会出错的
------解决方案--------------------
For I As Integer = 0 To 1
cmd.CommandText = "create table TT(F1 int,F2 int)" 是不是table名称重复了。
------解决方案--------------------
cmd.CommandText = "create table TT(F1 int,F2 int);"
试试
------解决方案--------------------
For I As Integer = 0 To 1