如何在获取文本框值时使用 datareader
嘿朋友,我正在使用带有*组合框*的表单,其中我从 sqldatabase 名为balance,该sqldatabase 有两列,一列是customername,另一列是 另一个现在是不平衡的。
在表单中,我绑定了表中的所有客户名称,现在我有一个文本框,我想做的是 当用户选择组合框时。所选项目中的任何一个客户名称我需要在文本框中显示所选客户的平衡
我已经使用了数据阅读器,但它显示错误你可以帮助我吗........
Dim ST As String = ComboBox1.SelectedItem
Dim sqlcon As New SqlConnection(conectionstring)
Dim sqlcmd As New SqlCommand("SELECT OBBALANCE FROM BALANCE WHERE CUSTOMERNAME = " & " '" & ST & "'" & "", sqlcon)
Dim sdr As New SqlDataReader
Try
con.Open()
sdr = sqlcmd.ExecuteReader
While (sdr.Read())
Textbox7.Text = sdr[1]
End While
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
我无法理解如何要读取请帮助我读取数据,表中第一个是列名,接下来是 obbalance 字段
Hey Friend i am using a form with a *combobox*in which the items i have taken from the
sqldatabase named as balance and that sqldatabase have two columns one is customername and the
another is obbalance now .
In the form i had binded all the customer names from the table and now i have a textbox i want to do is
when the user selects the combobox.selected item any one customer name i need to display the obbalance of the selected customer in tat textbox
Ihad used the datareader but it shows error can you plz help me........
Dim ST As String = ComboBox1.SelectedItem
Dim sqlcon As New SqlConnection(conectionstring)
Dim sqlcmd As New SqlCommand("SELECT OBBALANCE FROM BALANCE WHERE CUSTOMERNAME = " & " '" & ST & "'" & "", sqlcon)
Dim sdr As New SqlDataReader
Try
con.Open()
sdr = sqlcmd.ExecuteReader
While (sdr.Read())
Textbox7.Text = sdr[1]
End While
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
Icant understand how to read plz help me to read the data and in the table first is column name and next is obbalance field
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您要从数据库中读取单个值,因此请使用 ExecuteScalar 方法而不是使用数据读取器。
更新:
修改代码以检查 null。
删除了 datareader 对象,因为不再需要它。
从查询中删除了不必要的字符。
Since you are going read a single value from the database, use ExecuteScalar method instead of using a datareader.
Update:
Modified the code to check for null.
Removed the datareader object as it is no longer required.
Removed unnecessary characters from query.