1 [ASP.net] Tạo trang đăng nhập Fri Jul 19, 2013 11:03 pm
VD ta có 1 CSDL như sau
+ Account
class Login
Bắt sự kiện click button login
Vậy là song.
+ Account
username | password |
admin | admin |
user | user |
class Login
- Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Login
/// </summary>
public class Login
{
public Login()
{
//
// TODO: Add constructor logic here
//
}
public bool isLogin(string username, string password)
{
string name = null;
string connectionstring = "Data source=.;username=sa;password=thoa;database=Test";
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
SqlCommand cmd = new SqlCommand("Select username from Account where username='" + username + "' and password='" + password + "'", con);
name = (string)cmd.ExecuteScalar();
con.Close();
con.Dispose();
if (name == null)
return false;
else
return true;
/*Chương trình truy xuất vào CSDL và kiểm tra xem có Account nào thỏa cả 2 điều kiện không ,nếu có thì trả lạ
giá trị cho biến name. Lúc này biến nam sẽ bị thay đổi.
* Còn trường hợp không tìm thấy thì biến name sẽ không thay đổi. Như vậy sau khi kiểm tra trong CSDL xong, nếu biến name
* thay đổi, tức là tìm thấy thì return true còn ngược lại thì return false;
*/
}
}
Bắt sự kiện click button login
- Code:
protected void btnlogin_Click(object sender, EventArgs e)
{
Login login = new Login();
if(login.isLogin(txtusername.Text,txtpassword.Text)==true)
//Đăng nhập thành công
else
//Đăng nhập thất bại
}
Vậy là song.