类连接数据库mysql vb net 2013

时间:2014-11-20 11:37:37

标签: mysql vb.net datagridview

我已经为VB .NET 2013的mysql数据库建立了简单的类连接。但是我仍然不知道如何将数据库中的结果填充到datagridview。

这是我的数据库连接类VB .NET 2013。

Imports MySql.Data.MySqlClient
Imports MySql.Data

Public Class MainClass
    Public SQLConn = New MySqlConnection()
    Public SQLQuery As String
    Public Data As DataGridView
    Public SQLCommand As New MySqlCommand
    Public SQLAdapter As New MySqlDataAdapter
    Public SQLData As New DataTable

Public Sub BukaDB()
    SQLConn.ConnectionString = "server=localhost;" _
            & "user id=root;" _
            & "password=root;" _
            & "database=tikindonew"
    SQLConn.Open()

End Sub

Public Sub ProsesQuery(SQLQuery, Data)
    SQLCommand.Connection = New MySqlConnection
    SQLCommand.CommandText = SQLQuery
    SQLAdapter.SelectCommand = SQLCommand
    SQLAdapter.Fill(SQLData)
    Data = SQLData
End Sub

Public Sub TutupDB()
    SQLConn.Close()
    SQLConn.Dispose()


End Sub
End Class

这是Form1中Button内的代码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim SQL As New MainClass()
        SQL.BukaDB()
        SQL.ProsesQuery("select * from stok_konos", DataGridView1.DataSource)
        SQL.TutupDB()
End Sub

问题是当我尝试将数据从数据库填充到DataGridView1时,我总是遇到错误。

感谢您的帮助。

0 个答案:

没有答案