我在打开数据读取器时遇到问题 [英] i have problem in opening datareader
本文介绍了我在打开数据读取器时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在写一个有关leitnerbox的程序.在我的程序中,我想在数据库的表中搜索单词.我有一个文本框,代码在text_changed事件中.这是我的代码,然后是错误.
Hello All,
I''m writing a program about leitnerbox. In my program I want to search words in my table in data base. I have a text box and my codes are in text_changed event. Here is my code amd then the error.
Private Sub TextBoxX3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxX3.TextChanged
Dim objcmd As SqlCommand
Dim sql As String
If CheckBoxX2.Checked = True Then
sql = "select * from leitnertable where word like '%" & TextBoxX3.Text & "%'"
End If
If CheckBoxX1.Checked = True Then
sql = "select * from leitnertable where firstmeaning or secondmeaning like '%" & TextBoxX3.Text & "%'"
End If
objcmd = New SqlCommand(sql, con)
If objcmd.Connection.State = ConnectionState.Closed Then objcmd.Connection.Open()
Dim dr As SqlDataReader
dr = objcmd.ExecuteReader
While dr.Read
Dim dt As New DataTable
dt.Load(dr)
DataGridView2.DataSource = dt
End While
End Sub
复选框1正在搜索意思,而复选框2正在搜索单词.
当我调试程序时,错误是:
Check box1 is searching in meaninngs and check box2 is searching in words.
When I debug the program yhe error is:
>在关闭阅读器后尝试调用Read的尝试无效.
Invalid attempt to call Read when reader is closed.
我不知道该怎么办.
请帮帮我.
在此先谢谢您.
I don''t know what to do.
Please help me.
Thanks in Advance.
推荐答案
您可以使用objcmd = New SqlCommand(sql,con)
实例化SqlCommand,但在任何地方都不能将con声明为SqlConnection.它是Global变量还是Form Level变量,并且使用正确的ConnectionString正确声明了吗?
You instantiate your SqlCommand with objcmd = New SqlCommand(sql,con)
but nowhere do you declare con as a SqlConnection. Is it a Global or Form Level variable, and is it declared properly, with the correct ConnectionString ?
您不应该调用DataReader.Read
方法:在Command.ExecuteReader
之后,您只需一次调用DataTable.Load
,然后关闭DataReader
.
You shouldn''t call theDataReader.Read
method: afterCommand.ExecuteReader
you have just to call onceDataTable.Load
and then close theDataReader
.
检查您的SQLCONNECTION.有问题.
Check your SQLCONNECTION. There is the problem.
这篇关于我在打开数据读取器时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文