|
|
|
login in c# ... ครับ...เลยอยากทำฟอร์มล็อคอินตรวจสอบ เฉพาะ user, pass ครับ |
|
|
|
|
|
|
|
Welcome to C#
|
|
|
|
|
Date :
2010-04-03 21:41:13 |
By :
วุดดี้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมลองเขียนเองมั่วๆอ่ะครับแต่ก็ใช้ได้
สร้าง Textbox1 >>รับค่าไอดี
Textbox2 >>รับค่าพาส
button1>>ปุ่มยืนยัน
ฐานข้อมูลในทีนี้ใช้ sqlserver2005 ชื่อตาราง USERLOGIN เก็บค่า 2 ฟิวคือ
Username และ Password
Code (C#)
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 Main
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private DataSet _dataset;
private SqlCommand _command;
private SqlDataAdapter _adapter;
SqlConnection _conn = new SqlConnection();
private string strcon ="เชื่อมต่อฐานข้อมูล";
private string pass = "";
private string name = "";
private void Login_Load(object sender, EventArgs e)
{
_conn = new SqlConnection(strcon);//เรียกเชื่อมต่อฐานข้อมูลที่มาจากตัวแปร strcon
_conn.Open();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("คุณไม่ได้ใส่ชื่อผู้ใช้");
textBox1.Focus();
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("คุณไม่ได้ใส่รหัสผ่าน");
textBox2.Focus();
return;
}
string sql = "SELECT * FROM USERLOGIN";
_command = new SqlCommand(sql, _conn);
_adapter = new SqlDataAdapter(_command);
_dataset = new DataSet();
_adapter.Fill(_dataset, "search");
string t1 = "";
string t2 = textBox1.Text.ToLower(); //ทำให้ค่าไอดีที่รับเข้ามาเป็นตัวเล็กให้หมด
int a = 0;
for (int i = 0; i < _dataset.Tables["search"].Rows.Count; i++)
{
pass = _dataset.Tables["search"].Rows[i]["Password"].ToString();
name = _dataset.Tables["search"].Rows[i]["Username"].ToString();
t1 = name.ToLower();//ให้ไอดีที่อยู่ในฐานข้อมูลเป็นตัวพิมพ์เล็กเพื่อนำไปตรวจสอบ
if ((t2.Equals(t1)) && (textBox2.Text.Equals(pass))) a++;//ถ้าไอดีและพาสตรงกันให้เพิ่มค่า a ไป 1 ค่า
}
if (a == 0)//แสดงว่าไม่มีที่ตรง
{
MessageBox.Show("รหัสหรือไอดีผิดใส่ข้อมูลใหม่");
//tbxsearchname.Focus();
textBox2.Focus();
textBox2.Text = "";
return;
}
if (a > 0)//แสดงว่าพาสกับไอดีตรง
{
MessageBox.Show("รหัสถูกต้อง");
//อยากให้มันโชว์อะไร เปิดหน้าอะไรต่อไปก็จัด
}
}
}
}
อาจจะดูเด็กไปหน่อย แต่ก็อยากช่วยเพื่อนงั้บ
|
|
|
|
|
Date :
2010-04-06 18:41:02 |
By :
เพิ่งเริ่ม |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace MSEquipment
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
SqlConnection Conn = new SqlConnection();
private void frmLogin_Load(object sender, EventArgs e)
{
string strConn = DBConnString.strConn;
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
}
private void cmdLogin_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("SELECT UserName,Password FROM MEMBER");
sb.Append(" WHERE (UserName=@UserName)");
sb.Append(" AND (Password=@Password)");
SqlParameter UserName = new SqlParameter("UserName", SqlDbType.VarChar);
SqlParameter Password = new SqlParameter("Password", SqlDbType.VarChar);
string sqlLogin;
sqlLogin = sb.ToString();
SqlCommand com = new SqlCommand();
SqlDataReader dr;
com.CommandType = CommandType.Text;
com.CommandText = sqlLogin;
com.Connection = Conn;
com.Parameters.Add(UserName).Value = txtUserName.Text;
com.Parameters.Add(Password).Value = txtPassword.Text;
dr = com.ExecuteReader();
if (dr.HasRows)
{
frmMain frm = new frmMain();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("UserName หรือ Password ที่คุณป้อน ไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtUserName.Text = "";
txtUserName.Focus();
txtPassword.Text = "";
}
dr.Close();
dr.Dispose();
}
private void cmdEnd_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
|
|
|
|
|
Date :
2010-05-05 02:19:42 |
By :
worlord |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|