c#调用存储过程实现登录界面详解
create proc Pro_Login
(
@UserName nvarchar(10),
@PassWord nvarchar(10)
)
as
select * from [User] UserName=@UserName and PassWord=@PassWord
class ConnectionString
{
public static string conStr = "Data Source=MyLove-PC;Initial Catalog=data;Integrated Security=True";
}
#region
//连接数据库配置字符串
using (SqlConnection con = new SqlConnection(ConnectionString.conStr))
{
con.Open();//打开数据库
//调用存储过程
using (SqlCommand cmd = new SqlCommand("Pro_Login", con))
{
//把文本框的值作为参数传给存储过程
cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 10).Value = textBox1.Text.Trim();
cmd.Parameters.Add("@PassWord", SqlDbType.VarChar, 10).Value = textBox2.Text.Trim();
//通过存储过程的方式执行
cmd.CommandType = CommandType.StoredProcedure;
//开始读取数据
using (SqlDataReader dr = cmd.ExecuteReader())
{
//如果读到用户名和密码,则调转到界面Form2
if (dr.Read())
{
this.Hide();
Form2 f2 = new Form2();
f2.Show();
}
//否则,提示错误
else
{
MessageBox.Show("用户名或者密码错误", "请重新输入", MessageBoxButtons.OK);
textBox1.Clear();
textBox2.Clear();
textBox1.Focus();
}
}
}
}
#endregion
4,界面测试