On my Previous post we discussed Mysql and VB.60. now in this post we try to connected vb net 2008 with my sql server. same as like on VB 6.0, before we start coding for my sql we must installed mysql connecter for .net, you can download Mysql Connector for .net via reliez.net.
after installed the connector on project workspace, we must add reference to mysql.data. on this project same as like previous post for accesing database with.net , i used a class for accesing and give command to database, but in this project the connection for accesing database with connection string for mysql. the syntax code for connection class like below :
Imports MySql.Data.MySqlClient
Public Class mydb
Private mycon As New MySqlConnection("server=localhost;user id=root; database=cdcol; pooling=false;Connection Timeout=5")
Private mytable As DataTable
Public Sub New()
mycon.Open()
mytable = New DataTable
End Sub
Public Function QueryDatabase(ByVal Query As String) As DataTable
Try
Dim objDataSet As New DataSet
Dim objDataTable As New DataTable
Dim objDataAdapter As New MySqlDataAdapter(Query, mycon)
objDataAdapter.Fill(objDataSet, "DefaultTable")
objDataTable = objDataSet.Tables("DefaultTable")
mycon.Close()
Return objDataTable
Catch ex As Exception
MessageBox.Show(ex.Message, "DB ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return mytable
End Try
End Function
Public Sub DatabaseCommand(ByVal CommandString As String)
Try
Dim objInsert As New MySqlCommand
objInsert.CommandText = CommandString
objInsert.Connection = mycon
objInsert.Connection.Open()
objInsert.ExecuteNonQuery()
mycon.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "DB ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
for navigate data record from table you can see on sample project. you can download in here . for database i use database cdcoll. this default database on my sql when you intall sql with XAMPP from http://nat32.com/xampp.

0 komentar:
Post a Comment