You are on page 1of 20

Programmingknowledge.blogspot.

com C# & Sql Server2008


---------------------------------------------------------------------------------------------------------------------
Login Database by C# & Sql Server 2008 Tutorial_(1)

Login Database

C# & Sql Server ႔ ႔


႔ Software requirement ႔ Software ။၄ ႔ Microsoft Visual Studio
2008 ႔ Microsoft Sql Server 2008 ႔ ။ ႕ Server Base ႔
Standalone Server ႕ ႔ ။
႔ Data ႕ Result
Database Sql Server ။
႔ Microsoft Visual Studio 2008 ႔ Microsoft Sql Server 2008 ႔ ႔
။ Software Install ။
Software ႕ Install ။
႔ ႕ Software Run
႔ ။ ႔ ။ ႔ ႔
(၄) ။၄ ႔ ….
1. Microsoft Visual Studio 2008 ႕ C# User Interface ။
2. Microsoft Sql Server 2008 Database ။
3. C# Sql Server Database ။
4. Coding ႔ ။

၁. Microsoft Visual Studio 2008 ႕ C# User Interface


႔ Microsoft Visual Studio 2008 C# User Interface
။ Visual C# Windows Forms Application
Name: Save ။
IDCheck_Tutorial Name ။

Fig.1
-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
C# Page Form ႔ ။ ႔ Form
႕ Login Data User Interface ႔
႔ Form ။

႔ C# ႕ Form
Toolbox, Solution Explorer ႔ Properties box ႔ ။ Toolbox ႔
botton Solution Explorer ႔
Project File ။ Porperties box
႔ Form


႔ Form Button Form ႕ Properties

Control Name Text Font Maximizebox formBorderStyle StartPosition
Form1 frmLogin Login Zawgyi-One, 8.25pt False FixedDialog CenterScreen
႔ Form ။၄ ႕ Result

Fig.2
Name: Form Coding ၄
႔ Form ။ Text: Form box ႕ Title
။ Font: Form Font ႔ Zawgyi-One ႔
။ Maximizebox: Maximize
။ ႔ ႔ False ႔ Max ။
FormBorderStyle: FixedDialog ႔ ႕ Form /
Fixed ။ StartPosition: CenterScreen
႔ Program Run Result From Box Screen ႕
႔ ။ Form ႕ Icon Properties box ႕ Icon
။ ။
။ ႔ Login ႕ TextBox(2) Button ႔ ႕
Properties ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
Control Name Text TabIndex PasswordChar
TextBox1 txtUserName 0
TextBox2 txtPassword 1 *
Button1 btnLogin Login 2
Label1 User Name:
Label2 Password:

Fig.3
Fig.3 TextBox, Label and Button ႔ ႕ Properties
။ Name ႔ Coding
႔ ႔ ။ keyword ႔
။ Text ႕
User ။
TabIntex ႔ Tab Key
႔ ။ Text Intex 0
Intex key ။ Login Box
Intex 0 User Name TextBox Selected
႔ User Name ႔ Tab ႔ Intex 1 Password TextBox
Selected ။ TabIntex Intex key
။ TabIntex ႔
႔ ႔ ။
႔ ႕ ။ Beginner
႔ ။
႔ ။
PasswordChar (*) ႔ Password TextBox
႕ Password (*) ။ Fig.3 Password
TextBox ။ (*) key
Character (@,#,$,&,….) ။
PictureBox ႔ Login Box User
႔ ။ ႕ ႕ ႔
။ ။ (၁) Microsoft
Visual Studio 2008 ႕ C# User Interface ႔ ။
႕ ႔ ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
၂. Microsoft Sql Server 2008 Database
႔ ႔ Microsoft SQL Server 2008 Database
႔ ႕ Table Store Procedure ။ SQL Server
Database Table Sql Command line ႔ Window System ႔
။ Sql Command Line ႔ programmingknowledge.blogspot.com
႔ Topic ႔ ။ Sql command
line Window System Database, Table,…

Database
႔ Project Database ။
Sql command line …
Create Database IDCheck;
Sql command line New Query command Run ။
Window System ….
1. Fig.4 Database Folder R-Click  New Database ။
Fig.5 ။

Fig.4 Fig.5
2. Database name: Project ၏ Database Name ။
IDCheck ႔ ။
3. Database Files: Path Project ၏ Database

4. OK ။
႔ Sql Server ႕ Database Folder IDCheck Database
႔ ။ Sql command line Window System
Result ။ ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
Table
႔ ႔ Database Login Table
IDCheck Databse ႔ ။ Sql command line …
CREATE TABLE Tbl_Login
(
ID int PRINARY KEY IDENTITY(1,1) NOT NULL,
UserName nvarchar(30) NULL,
Password nvarchar(20) NULL
);
(or)
Window System ႔ …

Fig.6 Fig.7
1. Fig.6 Table Folder R-Click  New Table… ။ Fig.7
Column Name, Data Type & Allow Null Fig.7 ။
2. ID Primary Key ID Row R-Click Primary Key ။
3. ၄ ID Column Click Column Properties Identity Specification
Fig.8 ။ Record
ID No ။
4. Save ။ Login ႔
Tbl_Login ႔ ။

Fig.8
။ ။ Identity int Data Type Count Data Type ႔ ။
UserName ႔ Password nvarchar() ႔ UserName ႔ Password
႕ record Unicode ႔ ။
႔ ႕ IDCheck Database Tbl_Login ID, UserName, Password Column
Field Table ႔ ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
Table ႔ Record
႔ ႕ UserName and Password ႔ Project
။ Default ႔ ႔ UserName and Password ။
Login ႔ Tbl_Login Table ႔ Default ႕
UserName and Password Record ။ .. Record
။ Sql command line ….
Insert Into Tbl_Login( ‘Admin’, ‘123’);
(or)
Window System ႔ ….

Fig.9 Fig.10
1. Fig.9 Sql Server  Database  IDCheck( Database Name)  Tbl_Login( Table
Name) R-Click  Edit Top 200 Rows Click ။ Fig.10 ။
2. Fig.10 UserName and Password Enter ။ ႔ ID
UserName and Password ႔ Enter
Auto (1) ႔ ။ ႔
Table ID Identity ႔ ။
3. ႔ Default ႔ ႕ User Naem and Password
Save ႔ ။ Save Fig.10
Execute SQL Button Click Save ။
႔ Table ႔ Record ။
႔ ႔ Default ႕ UserName and Password
။ Admin Account
User Account User Account
။ ႔ (၃) ႔။

၃.C# Sql Server Database


႔ ႔ Sql Server 2008 Database
C# ႔ ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
႔ ႔
။ ႕ ႕
႔ ။ … ႔ ႕ ႔။

Database Connection

Fig.13 Fig.11

Fig.12

1. Fig.11 C# ႕ Solution Explorer  IDCheck_tutorial( project name) Properties 


Settings.Settings Double-Click ။ Fig.12 ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
2. Fig.12 Name: database connection
။ IDCheckConString ။
Type: ႔ Scope: ။ Value: Click
Fig.13 ။
3. Fig.13 Data Source: Change Microsoft Sql Server(SqlClient) ။
Server Name: ။ Log on the server:
Use Windows Authentication ။ Sql Server Windows System
႔ ။ ႔ ႔
Server ႔ Name and Password Use Sql Server
Authentication ။ Select or enter the database name:
(၂) ႔ Database IDCheck

4. ႔ Sql Server IDCheck Database ႔ ႕
။ ႔ /
႔ Test Connection Button ။ Fig.13
Message Box Error
Message Box ။
5. OK ။

DataSet
Sql Server Project Dataset
။ Dataset Sql server
Database C# ႕ UGI ။

Fig.14 Fig.15
Table Dataset ။ ႕
Dataset Table ၄ Dataset ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
။ ႕
Error Trip Table Dataset
႔ ။ .. Database Table ။ ၄
Table ႔ record Insert, Update and Delete ။
Table Error ၄ Table ႔ Dataset
Table ႕ record
။ Dataset Table
Error
။ Dataset ႔ ႔
႕ ႔ ။
1. ႔ Dataset Folder ။ ႔
Dataset ႔ ။
Folder ႔ ။ .. Dataset Folder
႔ Fig.14 IDCheck_Tutorial( project name)  R-Click 
Add  New Folder ႔ ။ ႔ project Folder
။ ။ Dataset
Folder ႔ DatasetFolder ႔ ။
2. Dataset ။ Fig.15
Folder R-Click  Add  New Items… ႔ ။ Fig.16 ။

Fig.16
3. Fig.16 Dataste Name: Add Click ။
Login Dataset ႔ DsLogin.xsd ႔ ။
႔ DsLogin.xsd Dataset Page ။ Dataset
IDCheck Database Tbl_Login Table ႔ ။ ႔။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
Table Adapter ( ႔) Dataset ႔ Table
႔ DsLogin Dataset Tbl_Login
Table ။ ။ ႔
႔ ။ ႔ ႔
။ … ႔..။

Fig.17 Fig.18
1. Fig.17 Dataset Page R-Click  Add  TableAdapter...  Click
။ Fig.18 ။
2. Which data connection should your application use to connect to the database ?
Database Connection (Fig.12) ႔ Connection Name
Next ။ Fig.19 ။

Fig.19 Fig.20
-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
3. Use SQL statements Selected Next ။ Sql command line
Fig.20 ။
4. Fig.20 Select * From Tbl_Login ႔ command line Next ။
Tbl_Login Table Column Field ႔
။ Fig.21 ။

Fig.21 Fig.22
5. Fig.21  Next  Fig.22  Finish ။

Fig.23
Fig.23 Tbl_Login Table ႕ Table Adapter ။
႔ Sql Procedure ။

Sql Stored Procedure


႔ Login ႕ UserName and Password
Database ႔ Procedure
။ ႔ UserName and Password Project ။
Error Message ။ … ႔ Procedure ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------

Fig.24 Fig.25
1. Fig.24 Tbl_LoginTableAdapter R-Click Add Query.. ။ Fig.25 ။
2. Fig.25 Create new stored procedure Selected Next ။

Fig.26 Fig.27
3. ႔ ႔ Database ႔ Login Record /
Fig.26 SELECT which returns a single value Selected Next
။ Sql procedure ႔ Fig.27 ။
4. Command line Next ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
Select Count(*) From Tbl_Login
Where UserName=@Name And Password=@Pw;
႔ Where ႔ ။ Where Table UserName = @
႔ ႕ UserName ။ ႔ UserName Database Table
႔ ။ Count ။ Count=1
Success ။ Count=0 UserName - Project ႔ ။ Password
။ ႔ ။ And connection
႔ ။ Error ။

Fig.28 Fig.29
5. Fig.28 What do you want to name the new stored procedure ?
Next  Fig.29 Next ။

Fig.30 Fig.31

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
6. Fig.30 Finish ။ Name and Password ႔ CheckLogin
Stored Procedure ႔ Tbl_LoginTableAdapter ။
႔ (၃) Sql Server ႔ C# ႔ Connection
(၃) ႔ ႔ ။ ။ C#
coding Sql command line ႔ ႔ ။
႔ command line ႔ Sql C#
coding ႔
႔ ။
။ ႔ ။

၄.C# Coding
႔ ။
Login by Database ႕ ။
C# Coding ။ Namespace ႔ ႔။
using System.Data.SqlClient;
႔ ႔ SQL Server ႔ Sql command line C# Coding
Namespacea ။
႔ Dataset TableAdapter coding
variable ။
DataSetFolder.DsLoginTableAdapters.Tbl_LoginTableAdapter tblLogin = new
DataSetFolder.DsLoginTableAdapters.Tbl_LoginTableAdapter();
႔ DatasetFolder Tbl_LoginTableAdapter
tblLogin Variable ။
႔ Coding Database ႔
tblLogin Variable ႔ Tbl_LoginTableAdapter
C# Coding ။
TableAdapter tblLogin
႔ Sql Server ႔ connection ။

SqlConnection conn = new SqlConnection(Properties.Settings.Default.IDCheckConString);
conn.Open();
command line ။ Sql Server ႔ conn variable
။ Coding (Fig.11,12,13) ႔
။ ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
C# Coding Sql Server ႔ Connection ။ ႔ ႕
Login ႕ UserName and Password / ႕ Login Button Coding ႔ ။
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text != "" & txtPassword.Text != "")
{
int chkLogin = int.Parse(tblLogin.CheckLogin(txtUserName.Text, txtPassword.Text).ToString());
if (chkLogin == 1)
{
MessageBox.Show("Your Login is Successful", "Login");
}
else
{
MessageBox.Show("Incorrect User Name and Password, please Try again!!","Login");
}
}
else
{
MessageBox.Show("Enter User Name and Password in TextBox","Login");
}
}
If Statement ႔ if (txtUserName.Text != "" & txtPassword.Text != "")
။ IF statement {…} else{…}
Enter User Name and Password in TextBox Message Box ။
int chkLogin = int.Parse(tblLogin.CheckLogin(txtUserName.Text, txtPassword.Text).ToString());
command line ႔ keyboard UserName(txtUserName.Text)
Password(txtPassword.Text) Database / CheckLogin Stored Procedure
။ (Count=1 or Count=0) ႔ Procedure
Record Database 1 0 ။ C# Data
String type ႔ ။ ႔ If(chkLogin==1) int Type ႔
int.Parse() ႔ CheckLogin Procedure Resurt int Type
chkLogin variable ။ ႔ ။
if (chkLogin == 1)
{
MessageBox.Show("Your Login is Successful", "Login");
}
else
{
MessageBox.Show("Incorrect User Name and Password, please Try again!!","Login");
}
command line ႔ int Type chkLogin
variable if statement ႔ ။ Result 1
႔ UserName and Password Record Database ။
Your Login is Successful Message Box ႔ if (chkLogin == 1)

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
Result 0 else statement Incorrect User Name and
Password, please Try again!! Message Box ။ Coding
႔ ။ ႔ Login Project ႕
Screen Shoot ႔ ။

Fig.32 Fig.33
Fig.32 ႔ User Name: and Password: Login Button
Click ။ Fig.33 User Name: Password:

Fig.34
Fig.34 ႔ keyboard UserName and Password Login
႔ Default UserName and Password ႔ Success
။ ႔ ႔

Login Project Coding ….
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;
namespace IDCheck_Tutorial
{

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
public partial class frmLogin : Form
{
DataSetFolder.DsLoginTableAdapters.Tbl_LoginTableAdapter tblLogin = new
DataSetFolder.DsLoginTableAdapters.Tbl_LoginTableAdapter();
public frmLogin()
{
SqlConnection conn = new SqlConnection(Properties.Settings.Default.IDCheckConString);
conn.Open();
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)


{
if (txtUserName.Text != "" & txtPassword.Text != "")
{
int chkLogin = int.Parse(tblLogin.CheckLogin(txtUserName.Text, txtPassword.Text).ToString());
if (chkLogin == 1)
{
MessageBox.Show("Your Login is Successful", "Login");
}
else
{
MessageBox.Show("Incorrect UserName and Password, please Try again!!","Login");
}
}
else
{
MessageBox.Show("Enter User Name and Password in TextBox","Login");
}
}
}
}
႔ Login Record
Message Box ႔ Result ။
႔ ။ Login
Message Box ႔ Form ။
႔ Form ႔ ။ Form

1. Fig.35 ။ IDCheck_Tutorial( Project Name)  Add  Windows Form…  Fig.36

2. Name: Add ။ Main Page ႔
frmMain.cs ႔ ။ ႔ Project File frmMain Form
႔ ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------

Fig.35 Fig.36
3. ႔ ( )၏ Data Database ႔
Form Frame ။

Fig.37
frmMain Form ႕ Form Properties
။ Login frmMain Form
႔ ။ frmMain Form ။

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
Control Name Text Font Maximizebox formBorderStyle StartPosition
Form2 FrmMain Entry ID Card Zawgyi-One, 8.25pt False FixedDialog CenterScreen
Properties ။ frmLogin.cs
။ Name: Coding ႔ ႔
။ … ႔ frmMain.cs Coding ။
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text != "" & txtPassword.Text != "")
{
int chkLogin = int.Parse(tblLogin.CheckLogin(txtUserName.Text, txtPassword.Text).ToString());
if (chkLogin == 1)
{
this.Hide();
frmMain ShowMain = new frmMain();
ShowMain.ShowDialog();
}
else
{
MessageBox.Show("Incorrect UserName and Password, please Try again!!","Login");
}
}
else
{
MessageBox.Show("Enter User Name and Password in TextBox","Login");
}
}
Coding ႔ ။ login
Message Box ။
this.Hide();
frmMain ShowMain = new frmMain();
ShowMain.ShowDialog();
this.Hide() ႔ frmLogin.cs Hide ႔
။ command line Login frmLogin Form ။
frmMain ShowMain = new frmMain(); C# ႕ Method ။ ႔ frmMain
႔ Main Page ShowMain Variable ။
ShowMain.ShowDialog(); ႔ command line ShowMain
Variable Screen frmMain Form ။
႔ ။

Fig.38
-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။
Programmingknowledge.blogspot.com C# & Sql Server2008
---------------------------------------------------------------------------------------------------------------------
User Name = Admin
Password = 123

Fig.39
Fig.38 User Name and Password Fig.39 ။
Incorrect UserName and Password, please Try again!! ။ ႔

႔ User Account
User Account ႕ Password Change ။
႔ ႔ ႔ ႔ ။

C# & Sql Server 2008 (1 ။ ႔



႔ ။
႔ ။
႔ ၏ ႔… ႔၏
႔… ။
Tay Zar Lin
Koyinmaung007@gmail.com
Programmingknowledge.blogspot.com

-----------------------------------------------------------------------------------------------------------
႔၏ ႔…။