如果我运行我的代码这个错误:连接没有关闭。连接当前状态是打开来了如何调试它 [英] if I run my code the this Error:The connetion was not closed.The connetion current state is open is came How to debugg it

查看:66
本文介绍了如果我运行我的代码这个错误:连接没有关闭。连接当前状态是打开来了如何调试它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmd.Connection = cm

cm.Open()

cmd.Connection = cm

cmd.CommandText =" SELECT * FROM studdetail WHERE name ='" &安培; ComboBox2.Text& '按名称订购'

dr = cmd.ExecuteReader()

如果dr.HasRows那么

while(dr.Read() )

ComboBox2.Text = dr!name

TextBox1.Text = dr!Regno

TextBox2.Text = dr!class

TextBox3.Text = dr!division

ComboBox2.Items.Add(dr!name)

'DateTimePicker1.Value = dr!d1

'TextBox4.Text = dr!kannada

结束时

结束如果

dr.Close()

cm.Close()

End Sub

请帮助如何调试此错误:连接未关闭。连接当前状态为打开

cmd.Connection = cm
cm.Open()
cmd.Connection = cm
cmd.CommandText = "SELECT * FROM studdetail WHERE name= '" & ComboBox2.Text & "' ORDER by name"
dr = cmd.ExecuteReader()
If dr.HasRows Then
While (dr.Read())
ComboBox2.Text = dr!name
TextBox1.Text = dr!Regno
TextBox2.Text = dr!class
TextBox3.Text = dr!division
ComboBox2.Items.Add(dr!name)
'DateTimePicker1.Value = dr!d1
'TextBox4.Text = dr!kannada
End While
End If
dr.Close()
cm.Close()
End Sub
Please Help How to debug this Error:The connetion was not closed.The connetion current state is open

推荐答案

在没有访问您的机器和网络的情况下进行调试很困难 - 如果不是不可能的话 - 我们无法在此处复制问题,因为我们没有足够的信息。

B.我想起了几件事情。

1)不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

2)在整个代码周围放置一个Try ... Catch块并报告问题(如果有的话)。有可能在其他地方引起这个问题。

3)使用使用块自动关闭和处理你的对象。

4)这是一个奇怪的命令你在那里:ORDER BY是多余的,因为它只返回具有相同名称的行...

That's difficult - if not impossible - to debug without access to your machine and network, we can't replicate the problem here because we don't have enough information.
But a couple of things do spring to mind.
1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) Put a Try...Catch block around that whole code and report what the problem (if any) is. Chances are there is a problem elsewhere which is causing this.
3) Use Using blocks to automatically Close and Dispose your objects.
4) That's an odd command you have there: The ORDER BY is redundant because it only returns rows with the same name anyway...
Try
    Using con As New SqlConnection(strConnect)
    	con.Open()
        Using com As New SqlCommand("SELECT * FROM studdetail WHERE [name]=@NM", con)
            com.Parameters.AddWithValue("@NM", ComboBox2.Text);
            Using dr As SqlDataReader = com.ExecuteReader()
                While dr.Read()
                    ComboBox2.Text = dr!name
                    TextBox1.Text = dr!Regno
                    TextBox2.Text = dr!class
                    TextBox3.Text = dr!division
                    ComboBox2.Items.Add(dr!name)
                End While
            End Using
        End Using
    End Using
Catch ex As Exception
    ' Report a problem using ex.Message
End Try



(而虽然应该是如果


这篇关于如果我运行我的代码这个错误:连接没有关闭。连接当前状态是打开来了如何调试它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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