天天看点

c#调用存储过程实现登录界面

1,创建存储过程

create proc Pro_Login

(

@UserName nvarchar(10),

@PassWord nvarchar(10)

)

as

select * from [User] [email protected] and [email protected]

2,通过类是实现配置数据库字符串连接

class ConnectionString

{

public static string conStr = "Data Source=MyLove-PC;Initial Catalog=data;Integrated Security=True";

}

3,实现登录功能

#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,界面测试

c#调用存储过程实现登录界面

转载于:https://www.cnblogs.com/thbbsky/archive/2013/03/17/2964384.html