'เชื่อมต่อฐานข้อมูล mysql
Public Const strConn As String = "Server=localhost;User Id=xxxx; Password=xxxx; Database=xxxx; Pooling=false; charset=utf8; Convert Zero Datetime=True; Persist Security Info=True"
Public Sub ConnectionDB()
conn = New MySqlConnection
Try
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Catch ex As Exception
Console.WriteLine("การเชื่อมต่อฐานข้อมูลล้มเหลว")
Exit Sub
End Try
End Sub
ตัวอย่างโค้ด insert
Code (VB.NET)
sb = New StringBuilder
sb.Append("INSERT INTO categories (name,description)")
sb.Append(" VALUES (@name,@description)")
strSQL = sb.ToString
cmd = New MySqlCommand
With cmd
.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtName.Text.Trim
.Parameters.Add("@description", MySqlDbType.Text).Value = txtDescription.Text.Trim
.CommandText = strSQL
.Connection = conn
.ExecuteNonQuery()
End With