如何从SQL数据库向数据库VB.NET SQL Server中具有相同ID的datagridview添加多行数据?

时间:2019-03-24 09:26:34

标签: sql sql-server vba vb.net datagridview

问题是我的代码从具有相同发票ID的数据库中检索了多行数据。但是,它仅在datagridview中显示第一行数据。

enter image description here

注意:发票ID不是主键,而是外键

我尝试使用数据读取器,它基于另一个Web来源。

Imports System.Data.SqlClient
Public Class payment
    Dim con As New SqlConnection("server = DESKTOP-SNQAJCQ;Database=SDP;Integrated security=SSPI")
    Dim dr As SqlDataReader
    Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click

        Dim i As Integer
        con.Open()

        Dim searchQuery As String = "SELECT * From Order_ WHERE id=@id "

        Dim cmd As New SqlCommand(searchQuery, con)
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = txtinvoice.Text
        dr = cmd.ExecuteReader
        While dr.Read
            i += 1
            dgwPayment.Rows.Add(dr.Item("item_description").ToString,
                                dr.Item("unit_price").ToString,
                                dr.Item("weight_").ToString,
                                dr.Item("quantity").ToString,
                                dr.Item("total").ToString,
                                dr.Item("special_remarks").ToString)
        End While

    End Sub
End Class

我的预期结果是它应该显示具有相同发票ID的所有数据行。

enter image description here

0 个答案:

没有答案