อันนี้เป็น Connection String ของ MySQL กับ ADO.NET
ตัวอย่างนี้จะประยุกต์ใช้กับ Form Login มีทั้ง VB.NET และ C#
frmLogin.vb
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmLogin
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim strConnString, strSQL As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=database; Pooling=false"
objConn.ConnectionString = strConnString
objConn.Open()
Dim intNumRows As Integer
strSQL = "SELECT COUNT(*) FROM member WHERE Username = '" & Me.txtUsername.Text & "' AND Password = '" & Me.txtPassword.Text & "' "
objCmd = New MySqlCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
If intNumRows > 0 Then
Dim frm As New frmMain
frm._strUser = Me.txtUsername.Text
frm.LoadInfor()
frm.Show()
Me.Hide()
Else
MessageBox.Show("Username or Password Incorrect")
End If
objConn.Close()
objConn = Nothing
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
If MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Application.Exit()
End If
End Sub
End Class
frmMenu.vb
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmMain
Dim strUser As String
Public Property _strUser() As String
Get
Return strUser
End Get
Set(ByVal value As String)
strUser = value
End Set
End Property
Public Sub LoadInfor()
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim dtAdapter As MySqlDataAdapter
Dim dt As New DataTable
Dim strConnString, strSQL As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=database; Pooling=false"
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = "SELECT * FROM member WHERE Username = '" & strUser & "' "
dtAdapter = New MySqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
Me.lblUser.Text = strUser
Me.lblName.Text = dt.Rows(0)("Name")
Me.lblEmail.Text = dt.Rows(0)("Email")
End If
objConn.Close()
objConn = Nothing
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Dim frm As New frmLogin
frm.Show()
Me.Hide()
End Sub
End Class
Code สำหรับภาษา C#
frmLogin.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
namespace LoginFormCS
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
MySqlConnection objConn = new MySqlConnection();
MySqlCommand objCmd = new MySqlCommand();
string strConnString = null;
string strSQL = null;
strConnString = "Server=localhost;User Id=root; Password=root; Database=database; Pooling=false";
objConn.ConnectionString = strConnString;
objConn.Open();
int intNumRows = 0;
strSQL = "SELECT COUNT(*) FROM member WHERE Username = '" + this.txtUsername.Text + "' AND Password = '" + this.txtPassword.Text + "' ";
objCmd = new MySqlCommand(strSQL, objConn);
intNumRows = Convert.ToInt32(objCmd.ExecuteScalar());
if (intNumRows > 0)
{
frmMain frm = new frmMain();
frm._strUser = this.txtUsername.Text;
frm.LoadInfor();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("Username or Password Incorrect");
}
objConn.Close();
objConn = null;
}
private void btnClose_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Application.Exit();
}
}
}
}
frmMenu.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
namespace LoginFormCS
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
string strUser;
public string _strUser
{
get { return strUser; }
set { strUser = value; }
}
public void LoadInfor()
{
MySqlConnection objConn = new MySqlConnection();
MySqlCommand objCmd = new MySqlCommand();
MySqlDataAdapter dtAdapter = default(MySqlDataAdapter);
DataTable dt = new DataTable();
string strConnString = null;
string strSQL = null;
strConnString = "Server=localhost;User Id=root; Password=root; Database=database; Pooling=false";
objConn.ConnectionString = strConnString;
objConn.Open();
strSQL = "SELECT * FROM member WHERE Username = '" + strUser + "' ";
dtAdapter = new MySqlDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
this.lblUser.Text = strUser;
this.lblName.Text = dt.Rows[0]["Name"].ToString();
this.lblEmail.Text = dt.Rows[0]["Email"].ToString();
}
objConn.Close();
objConn = null;
}
private void btnBack_Click(object sender, EventArgs e)
{
frmLogin frm = new frmLogin();
frm.Show();
this.Hide();
}
}
}