它显示即使我的数据库中有数据[没有数据时无效尝试读取] 我希望在插入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
答案 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