如何使用vb.net显示从mysql到datagridview的数据,并显示默认列名

时间:2016-02-20 00:51:37

标签: mysql vb.net datagridview

这是我的datagridview Screenshot of Datagridview我需要帮助。如何将mysql数据库的数据显示到我的datagridview,它具有我已经设置的默认标题文本或列名。我使用搜索按钮。我使用此代码从不同的表查询,我的默认列名称不会放置数据。任何人都可以建议我使用正确的代码来显示我的数据

Dim conn As New MySqlConnection
        conn.ConnectionString = ("server=127.0.0.1;user id=root;password=12345;database=dbsis3bkenth;")
        Try
            conn.Open()
            For k As Integer = 0 To DataGridView1.Rows.Count - 2
                sql = "SELECT (LName,FName,MI) FROM tblsisterbrother where IDNoBrodSis = '" & cbIDNo.Text & "'" & _
                    "VALUES ('" & DataGridView1.Rows(k).Cells("LN").Value.ToString & "', '" & DataGridView1.Rows(k).Cells("FN").Value.ToString & "', '" & DataGridView1.Rows(k).Cells("MI").Value.ToString & "');"
                cmd = New MySqlCommand(sql, conn)
                dr = cmd.ExecuteReader
                dr.Read()
            Next

        If dr.HasRows = True Then
            MessageBox.Show("Record Found.!")
        Else
            MessageBox.Show("Record Unfound.!")
        End If
        dr.Close()
    Catch ex As MySqlException
        MessageBox.Show("Error in searching to database:error is:" & ex.Message)
        Exit Sub
    End Try
    dr.Close()
    Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter
    DataAdapter1.SelectCommand = cmd
    DataAdapter1.Fill(ds, "tblsisterbrother")
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "tblsisterbrother"
    conn.Close()

0 个答案:

没有答案