PDA

View Full Version : سوال: ایجاد فرم Login؟



parinaz63
سه شنبه 24 شهریور 1388, 12:46 عصر
چگونه می توان صحت اطلاعات user و password را چک کرد؟

ali_md110
سه شنبه 24 شهریور 1388, 14:49 عصر
تابع زیر یک مقدار بولی بر میگردونه

Boolean logins()
{

connection = new SqlConnection();
connection.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirecto ry|\\Db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
connection.Open();
string sql;
sql = "select UserName,Pass from login where username=@username and pass =@password";
command = new SqlCommand();
command.CommandText = sql;
command.Connection = connection;
command.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = txtUserName.Text;
command.Parameters.Add("@password", SqlDbType.NVarChar, 50).Value = txtPassword.Text;
reader = command.ExecuteReader();
if (reader.HasRows == true)
{
return true;
}
else
{
return false;
}

reader.Close();
reader = null;
connection.Close();
}
دو تا تکست باکس و یک دکمه نیاز داری


private void btnlogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Length == 0)

{
MessageBox.Show("نام کاربری را وارد کنید");
return;//از ادامه اجرای کد صرفه نظر کن
}
if (txtPassword.Text.Length == 0)
{
MessageBox.Show("رمز کاربری را وارد کنید");
return;

}
if (logins() == true)
{
this.Close();

}
else
{
MessageBox.Show("نام کاربری و پسورد اشتباه است");
}
}