附加信息无效的操作。连接已关闭。
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
系列文章
更多
同类精品
更多
-
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
复制文件夹/文件而不修改属性?
it1352 07-15 -
为什么字符'^'被Python Popen忽略 - 在Popen Windows转义'^'字符?
it1352 08-07