|
|
|
การ LOGIN ด้วย C# + SQL Server เเบบง่ายๆ ที่ใครๆก็สามารถก็ทำได้ ! |
|
|
|
|
|
|
|
พอดีกำลังเริ่มทำโปรเจครายวิชา วันนี้ทำหน้า Login ได้ เลยเอาเเบ่งปัน
กว่าจะ Get ก็เอาซ่ะ งง ตั่งนาน...อิอิ...
Code (C#)
using System.Data.SqlClient;
namespace Project_Pm2
{
public partial class frmlogin : Form
{
public frmlogin()
{
InitializeComponent();
}
private SqlConnection conn;
private void Form1_Load(object sender, EventArgs e)
{
txtId.Focus();
}
private void connectDB()
{
string conStr = "Data Source = BIGGEST-PC; Initial Catalog = db_motorcycle; User = user1; Password = 1234 ";
conn = new SqlConnection();
conn.ConnectionString = conStr;
if (conn.State == ConnectionState.Open)
{ conn.Close(); }
conn.Open();
}
private void button1_Click(object sender, EventArgs e)
{
connectDB();
SqlCommand comm = new SqlCommand();
SqlDataReader dr;
comm.Connection = conn;
comm.CommandText = "Select emp_id , emp_password From employee Where emp_id=@ID and emp_password=@PASS ";
comm.Parameters.Add("@ID", SqlDbType.VarChar).Value = txtId.Text;
comm.Parameters.Add("@PASS", SqlDbType.VarChar).Value = txtPass.Text;
string ID = txtId.Text;
dr = comm.ExecuteReader();
if (dr.HasRows)
{
MessageBox.Show("กำลังเข้าสู่หน้าหลักโปรเเกรมโดย "+ID.ToString());
frmMain objfrmMain = new frmMain(ID);
objfrmMain.Show();
this.Hide();
}
else
{
MessageBox.Show("รหัสเข้าใช้งานไม่ถูกต้อง");
txtId.Focus();
txtId.Clear();
txtPass.Clear();
}
conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("กำลังออกจากโปรเเกรมใช่หรือไม่.?","Exit", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
}
}
}
Tag : .NET, MySQL, C#, VS 2008 (.NET 3.x)
|
|
|
|
|
|
Date :
2013-01-23 01:17:11 |
By :
biggest59 |
View :
4640 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|