搜索在vb中读取sql数据到文本框

时间:2014-12-20 07:51:59

标签: sql-server vb.net

它显示即使我的数据库中有数据[没有数据时无效尝试读取] 我希望在插入txtid.text的文本框中显示详细信息

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


     Dim con As New SqlConnection
     Dim cmd As New SqlCommand
     Dim rd As SqlDataReader
    Try

        con.ConnectionString = "Server=KAVIER;Database=vb;Trusted_Connection=True;"
        con.Open()
        cmd.Connection = con


        cmd.CommandText = "SELECT * FROM music where mus_id  = '" & txtid.Text & "' "
        rd = cmd.ExecuteReader() 'corrected my previous mistake

        If rd.HasRows Then

            txtartist.Text = rd.Item("customer")
            txtid.Text = rd.Item("album")
            txtgenre.Text = rd.Item("genre")
            hided.Text = rd.Item("music_copies")
            txtprice.Text = rd.Item("price")
            txttotal.Text = txtprice.Text * txtwalkinquantities.Text

        End If
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

1 个答案:

答案 0 :(得分:0)

cmd.CommandText = "SELECT * FROM music where mus_id  = '" & txtid.Text & "' "
        rd = cmd.ExecuteReader()

    If rd.read Then

        txtartist.Text = rd.Item("customer")
        txtid.Text = rd.Item("album")
        txtgenre.Text = rd.Item("genre")
        hided.Text = rd.Item("music_copies")
        txtprice.Text = rd.Item("price")
        txttotal.Text = txtprice.Text * txtwalkinquantities.Text

    End If

rd.HasRows替换为rd.read