在vb.net中执行阅读器问题 [英] Execute Reader Problem in vb.net
问题描述
我是vb.net的新手,正在从事一个项目,该项目显示了从ms sql db到listview的数据.但是,我遇到了问题,陷入了如下问题:
Hi,
I am new to vb.net and i am working on a project which shows data from the ms sql db to the listview. However, i am having a problem and got stucked as below:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lvwTest.View = View.Details
lvwTest.Columns.Add("Customer Name", 100, HorizontalAlignment.Left)
Dim command As SqlCommand = _
New SqlCommand("SELECT CustName FROM Report", Connection)
Connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
Do While (reader.Read())
lvwTest.Items.Add(reader.GetInt32(0))
Loop
reader.Close()
Connection.Close()
End Sub
有人可以帮我解决这个问题.
非常感谢.
Can someone help me out with this problem.
Thank you very much.
推荐答案
直接来自文档的示例;
Example straight from the docs;
Dim reader As SqlClient.SqlDataReader
Dim recordData As String = ""
Dim recordCount As Integer = 0
Dim i As Integer = 0
sqlConnection1.Open()
reader = sqlDataAdapter1.SelectCommand.ExecuteReader()
While reader.Read()
For i = 0 To reader.FieldCount - 1
recordData &= reader(i).ToString()
recordData &= "-"
Next
recordData &= ControlChars.CrLf
recordCount += 1
End While
sqlConnection1.Close()
MessageBox.Show("Records processed: " & recordCount)
MessageBox.Show("Data:" & ControlChars.CrLf & recordData)
但是,我将使用一个绑定源,然后将控件绑定到该源.
在MSDN上有一个很好的视频,关于基于数据的表单",并且该系列中还有其他一些值得一看.
http://msdn.microsoft.com/en-gb/vbasic/bb725824.aspx [ ^ ]
However, i would use a bindingsource, and then bind the controls to that.
There is a good video on MSDN on Forms over Data, and there are a few others in the series that are worth watching.
http://msdn.microsoft.com/en-gb/vbasic/bb725824.aspx[^]
您没有确切说明问题所在,因此很难说.
但是,我确实注意到您正在尝试读取一个整数值,并且列表视图列的名称(客户名称")表明CustName字段是varchar/string.
在这种情况下,您应该使用reader.GetString(0)而不是reader.GetInt32(0)
祝你好运,
约翰尼·J.
You don''t specify exactly what the problem is, so it''s hard to say.
I do however notice that you''re trying to read an integer value, and the name of your listview column ("Customer Name") suggests that the CustName field is a varchar/string.
In that case you should use reader.GetString(0) instead of reader.GetInt32(0)
Good luck,
Johnny J.
这篇关于在vb.net中执行阅读器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!