สอบถามเพื่อนๆ ครับพอดีจะเขียน visual basic 2010 แล้วเชื่อมต่อกับฐานข้อมูล MySQL แต่ติด Error ตลอด
ตัวเชื่อมต่อกับ database mySQL ถูกไหม Code (VB.NET)
Module DBConnString
Public strConn As String = "Server=10.10.10.74;Port=3306;Database=nmu_carpark;Uid=root;Pwd=ooiJPdmZr96CP14g6dq2"
End Module
ลองเขียนส่วนของโค๊ดที่เป็นใน department มาใส่ใน combo box Code (VB.NET)
Imports System.Text
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class EmployeeToCar
Dim MySQLConn As New MySqlConnection
Dim com As New MySqlCommand
Dim dr As MySqlDataReader
Dim dtCheck As DataTable
#Region ".:: ClearALL ::."
Sub ClearALL()
Me.CB_Department.Text = ""
Me.ComboBoxDepartment()
End Sub
#End Region
Sub ConnectDB()
strConn = DBConnString.strConn
With MySQLConn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
End Sub
#Region ".:: COMBO BOX MANAGEMENT ::."
Sub ComboBoxDepartment()
Try
Me.ConnectDB()
Dim sb As New StringBuilder
sb.Append("SELECT * FROM department;")
Dim sqlCheck As String
sqlCheck = sb.ToString()
With com
.CommandText = sqlCheck
.CommandType = CommandType.Text
.Connection = MySQLConn
dr = .ExecuteReader()
If dr.HasRows Then
dtCheck = New DataTable()
dtCheck.Load(dr)
Me.CB_Department.Items.Clear()
With Me.CB_Department
.BeginUpdate()
.DisplayMember = "dep_name"
.ValueMember = "dep_id"
.DataSource = dtCheck
.EndUpdate()
End With
Else
Me.CB_Department.DataSource = Nothing
End If
End With
dr.Close()
Catch ex As Exception
Exit Try
Exit Sub
End Try
End Sub
#End Region
Private Sub EmployeeToCar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GetDBdata()
Me.ClearALL()
End Sub
End Class