附加信息:无效的操作。连接已关闭。 [英] Additional information: Invalid operation. The connection is closed.

查看:85
本文介绍了附加信息:无效的操作。连接已关闭。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有两个表。我正在使用这些表来探索事务,插入,更新,删除,回滚和提交的应用程序。我有四个文本框两个到每个表和一个命令
按钮。我试图插入这些表并得到以下"附加信息:无效的操作。连接已关闭。
"带下划线的声明中的例外情况。我将非常感谢所有帮助。我的代码是
显示在下面

 Private Sub btnTransaction_Click(sender As Object, e As EventArgs) Handles btnTransaction.Click
        Using connection As New OleDbConnection(connstr)
            Dim Cmd As New oledbCommand
            Dim transaction As oledbtransaction
            'connection to New oledb Connection
            Cmd.connection = connection
            'open connection and execute transaction
            Try
                connection.open()

            Catch ex As Exception
                'Start Local connection
                transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
                'assign transaction Object to local 
                Cmd.connection = connection
                Cmd.Transaction = transaction
                'execute the commands
                Cmd.commandtext = "INSERT INTO RelInfo(Surname, FirstName) Value (@SName, @FName)"
                Cmd.Parameters.AddWithValue("@SName", txtSurname.Text)
                Cmd.Parameters.AddWithValue("@FName", txtFirstName.Text)
                Cmd.ExecuteNonQuery()

                Cmd.CommandText = "INSERT INTO StudentInfo(SName, FName) Value (@SName, @FName)"
                Cmd.Parameters.AddWithValue("@SName", txtSName.Text)
                Cmd.Parameters.AddWithValue("@FName", txtFName.Text)
                Cmd.ExecuteNonQuery()

                'commit transaction
                transaction.Commit()
                MsgBox("Both Records Are Written to the database")
                Console.WriteLine(ex.Message)
                'Try to rollback the transaction
                Try
                    transaction.Rollback()
                Catch
                    ' do nothing transaction is not active anymore

                End Try
            End Try
        End Using
        Dim response As DialogResult = MessageBox.Show("Already been executed." &
                        Environment.NewLine & _
                        "Proceed with transaction?", "Performing transanction", _
                        MessageBoxButtons.YesNo)
            End Sub
End Class

推荐答案

为什么你在catch中有所有代码然后在catch中有另一个try catch,它没有意义。我读这个的方式,如图所示进入catch,必须在外部try中抛出一个异常。
Why do you have all the code in the catch then have another try catch in that catch, it does not make sense. The way I read this, to enter the catch as shown there must be an exception thrown in the outer try.


这篇关于附加信息:无效的操作。连接已关闭。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆