如何在获取文本框值时使用 datareader

发布于 2024-11-03 02:16:40 字数 815 浏览 3 评论 0原文

嘿朋友,我正在使用带有*组合框*的表单,其中我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-11-10 02:16:40

由于您要从数据库中读取单个值,因此请使用 ExecuteScalar 方法而不是使用数据读取器。

Dim ST As String = ComboBox1.SelectedText 

Dim sqlcon As New SqlConnection(conectionstring)

Dim sqlcmd As New SqlCommand("SELECT OBBALANCE FROM BALANCE WHERE CUSTOMERNAME = '" & ST & "'" , sqlcon)
Dim result as Object

Try
    con.Open()

    result = sqlcmd.ExecuteScalar()

    If result IsNot Nothing Then
         Textbox7.Text = result.ToString() 
    End If

Catch ex As SqlException
    MessageBox.Show(ex.Message)     

End Try

更新:
修改代码以检查 null。
删除了 datareader 对象,因为不再需要它。
从查询中删除了不必要的字符。

Since you are going read a single value from the database, use ExecuteScalar method instead of using a datareader.

Dim ST As String = ComboBox1.SelectedText 

Dim sqlcon As New SqlConnection(conectionstring)

Dim sqlcmd As New SqlCommand("SELECT OBBALANCE FROM BALANCE WHERE CUSTOMERNAME = '" & ST & "'" , sqlcon)
Dim result as Object

Try
    con.Open()

    result = sqlcmd.ExecuteScalar()

    If result IsNot Nothing Then
         Textbox7.Text = result.ToString() 
    End If

Catch ex As SqlException
    MessageBox.Show(ex.Message)     

End Try

Update:
Modified the code to check for null.
Removed the datareader object as it is no longer required.
Removed unnecessary characters from query.

神也荒唐 2024-11-10 02:16:40
Dim ST As String = ComboBox1.SelectedText 

    Dim sqlcon As New SqlConnection(conectionstring)

    Dim sqlcmd As New SqlCommand("SELECT OBBALANCE FROM BALANCE WHERE CUSTOMERNAME =  " & " '" & ST & "'" & "", sqlcon)
  Dim sdr As SqlDataReader


    Try
        sqlcon.Open() 'edited
        sdr = sqlcmd.ExecuteReader()
        While (sdr.Read())
            TextBox1.Text = sdr("OBBALANCE").ToString() 'sdr(1) is wrong since there is only one item
        End While
    Catch ex As SqlException
        MessageBox.Show(ex.Message)


    End Try
Dim ST As String = ComboBox1.SelectedText 

    Dim sqlcon As New SqlConnection(conectionstring)

    Dim sqlcmd As New SqlCommand("SELECT OBBALANCE FROM BALANCE WHERE CUSTOMERNAME =  " & " '" & ST & "'" & "", sqlcon)
  Dim sdr As SqlDataReader


    Try
        sqlcon.Open() 'edited
        sdr = sqlcmd.ExecuteReader()
        While (sdr.Read())
            TextBox1.Text = sdr("OBBALANCE").ToString() 'sdr(1) is wrong since there is only one item
        End While
    Catch ex As SqlException
        MessageBox.Show(ex.Message)


    End Try
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文