• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

附加信息无效的操作。连接已关闭。

用户头像
it1352
帮助1

问题说明

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

 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

正确答案

#1
为什么你在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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhefafkg
系列文章
更多 icon
同类精品
更多 icon
继续加载