1:创建存储过程:
create proc Login
@UserName varch(50)
@PassWord int
as
select * from Login where userName = @UserName and passWord = @PassWord
2:创建一个方法Login.
public void Login()
{
conStr = "server = (local);trusted_connection = true; database = loginDataBase";
SqlConnection sqlCon = new SqlConnection();
try
{
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand("Login",conStr);
= CommandType.StoredProcedure;
SqlParameter userName = new SqlParameter("@UserName",SqlDbType.Varchar,50);
userName.Direction = ParameterDirection.Input;
userName.Value = txtUserName.Text;
SqlParameter passWord = new SqlParameter("@PassWord",SqlDbType.Int,4);
passWord.Direction = txtPassWord.Text;
passWord.Value = int.Pase(txtPassWord.Text);
sqlCom.Parameter.Add(userName);
sqlCom.Parameter.Add(passWord);
sqlCom.ExecuteNoQuery();
MessageBox.Show("登录成功!!!");
}
catch(SqlException sqlExcep)
{
MessageBox.Show(sqlExcep.Message());
}
catch(Exception excep)
{
MessageBox.Show(excep.Message());
}
finally
{
sqlCon.Close();
}
}
3:在"登录"按钮事件中调用方法Login
private void Login_Click(object sender, System.EventArgs e)
{
Login();
}