如果“连接未关闭”错误,请关闭连接? [英] Close connection if there is an error 'connection not closed'?
本文介绍了如果“连接未关闭”错误,请关闭连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的vb.net应用程序显示错误。
连接未关闭。连接的当前状态已打开
这是我的码。请帮我纠正错误:
My vb.net application shows an error.
"Connection not closed. Connection's current state is open"
Here is my code. Please help me rectify the error:
Private Sub saveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveBtn.Click
addSubject()
End Sub
Private Function addSubject() As Boolean
Dim cmd As New SqlCommand
Dim ok As Boolean = False
cmd.Connection = con
cmd.CommandText = " INSERT INTO Subjects(subject_name) VALUES(@inn)"
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("inn", subjectTxt.Text)
Try
con.Open()
cmd.ExecuteNonQuery()
ok = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Add failed")
ok = False
End Try
cmd.Dispose()
con.Close()
Return ok
con.Close()
End Function
推荐答案
嗨
ur尝试打开连接,但是该连接已经打开,它没有处于关闭状态,所以删除con.open并尝试
删除任何一个con.Close()表单catch块。
你在哪里声明con?你把con声明为公共变量吗?
Hi u r try to open connection,but that connection is already opened,it not in close state,so remove con.open and try
Remove any one con.Close() form catch block.
Where u declare con ? did u declare con as public variable ?
将连接声明为Public,然后如果有任何东西打开则先关闭连接然后打开它
declare connection as Public ,then First close connection if anything is open Then Open it
这篇关于如果“连接未关闭”错误,请关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文