<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="MySql.Data.MySqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As MySqlConnection
Dim objCmd As MySqlCommand
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
objConn = New MySqlConnection(strConnString)
objConn.Open()
If objConn.State = ConnectionState.Open Then
Me.lblText.Text = "MySQL Connected"
Else
Me.lblText.Text = "MySQL Connect Failed"
End IF
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - MySQL</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Imports System.Data
Imports System.Data.SqlClient
'Imports System.Data.OleDb
Public Class Form1
Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String = "select * from booktype"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
With conn ' ตัวแปรใช้ติดต่อฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = StrConn
.Open()
End With
TextBox1.Text = sql
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sql = TextBox1.Text
If Len(Trim(sql)) > 0 Then
Try
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "book")
DataGridView1.DataSource = ds.Tables("book")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MsgBox("กรุณาป้อนคำสั่ง")
End If
End Sub
End Class
ส่วน Module1
Module Module1
Public strConn As String = "Server=localhost;User Id=root; Password=1234; Database=salebook; Pooling=false"
Imports System.Data
Imports System.Data.SqlClient
'Imports System.Data.OleDb
Public Class Form1
Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String = "select * from booktype"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
With conn ' ตัวแปรใช้ติดต่อฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = StrConn
.Open()
End With
TextBox1.Text = sql
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sql = TextBox1.Text
If Len(Trim(sql)) > 0 Then
Try
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "book")
DataGridView1.DataSource = ds.Tables("book")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MsgBox("กรุณาป้อนคำสั่ง")
End If
End Sub
End Class