循环时从sql获取数据到datagridview?

时间:2019-10-02 01:08:36

标签: sql-server vb.net

当我从Excel打开文件并将其显示在dt_data中时,我试图这样做,我想选择数据库中是否存在dt_data中的特定列并显示结果在另一个datagridview dt_sample中。

我已经开始工作了,但是问题是仅显示dt_data中的第一行。

Try

       For i As Integer = 0 To dt_data.RowCount - 3
               Dim meter_number As String
               meter_number = dt_data.Rows(i).Cells(3).Value
               Dim query As String = "Select * from customer where meter_num = @meter_num"
               conn.Open()
               Dim command As New SqlCommand(query, conn)
               command.Parameters.AddWithValue("@meter_num", meter_number)
               Dim da As New SqlDataAdapter(command)
               Dim ds As New DataSet
               da.Fill(ds, "customer")
               dt_sample.DataSource = ds.Tables(i)

               conn.Close()

           Next
       Catch ex As SqlException
           MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
       Catch ex As Exception
           MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
           'End Try
           'Catch ex As Exception
           'MessageBox.Show(String.Format("Error: {0}", ex.Message), "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
       End Try

0 个答案:

没有答案