USE [mydatabase]
GO
/****** Object: Table [dbo].[member] Script Date: 03/12/2012 09:27:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[member](
[CustomerID] [varchar](4) NOT NULL,
[Username] [varchar](30) NULL,
[Password] [varchar](30) NULL,
[Name] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[CountryCode] [varchar](2) NULL,
[Budget] [float] NULL,
[Used] [float] NULL,
CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
/*-- Data */
INSERT INTO member (CustomerID, UserName, Password, Name, Email, CountryCode, Budget, Used)
VALUES ('C001', 'win', 'win123', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO member (CustomerID, UserName, Password, Name, Email, CountryCode, Budget, Used)
VALUES ('C002', 'john', 'john123', 'John Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO member (CustomerID, UserName, Password, Name, Email, CountryCode, Budget, Used)
VALUES ('C003', 'jame', 'jame123', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO member (CustomerID, UserName, Password, Name, Email, CountryCode, Budget, Used)
VALUES ('C004', 'chalee', 'chalee123', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);
นำ Query ไปรันหรือสร้าง Database ตามโครงสร้าง
โครงสร้างตาราง
ข้อมูลที่ Insert เข้าไป
SQL Server ConnectionString
Server=IP/Server;UID=sa;PASSWORD=;Database=database;Max Pool Size=400;Connect Timeout=600;
Connection String ของ SQL Server Database
ตัวอย่างนี้จะประยุกต์ใช้กับ Form Login มีทั้ง VB.NET และ C#
frmLogin.vb
Imports System.Data.SqlClient
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 SqlConnection
Dim objCmd As New SqlCommand
Dim strConnString, strSQL As String
strConnString = "Server=localhost;UID=sa;PASSWORD=;Database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
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 SqlCommand(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 System.Data.SqlClient
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 SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As SqlDataAdapter
Dim dt As New DataTable
Dim strConnString, strSQL As String
strConnString = "Server=localhost;UID=sa;PASSWORD=;Database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = "SELECT * FROM member WHERE Username = '" & strUser & "' "
dtAdapter = New SqlDataAdapter(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 System.Data.SqlClient;
using System.IO;
namespace LoginFormCS
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
string strConnString = null;
string strSQL = null;
strConnString = "Server=localhost;UID=sa;PASSWORD=;Database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
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 SqlCommand(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 System.Data.SqlClient;
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()
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = default(SqlDataAdapter);
DataTable dt = new DataTable();
string strConnString = null;
string strSQL = null;
strConnString = "Server=localhost;UID=sa;PASSWORD=;Database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn.ConnectionString = strConnString;
objConn.Open();
strSQL = "SELECT * FROM member WHERE Username = '" + strUser + "' ";
dtAdapter = new SqlDataAdapter(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();
}
}
}