【发布时间】:2016-10-14 16:14:42
【问题描述】:
我想把用户名和密码取到数据库中,根据插入的用户名和密码获取用户角色但是这段代码不起作用
public bool Login(out string Msg)
{
bool b = true;
Msg = "";
SqlConnection con = new SqlConnection(connection.connectstr);
try
{
con.Open();
SqlCommand com = new SqlCommand("user_proc", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@u_name", SqlDbType.NVarChar).Value = this.u_name;
com.Parameters.Add("@u_password", SqlDbType.NVarChar).Value = this.u_password;
com.ExecuteNonQuery();
con.Close();
b = true;
}
catch (Exception ex)
{
con.Close();
Msg = ex.Message;
b = false;
}
return b;
}
以及应该将角色检查到数据库中的 c# 代码,如果不是管理员,则将我重定向到服务器页面,如果不是,则将我重定向到客户端页面:-
protected void btn_login_Click(object sender, EventArgs e)
{
my_user u = new my_user();
u.u_name = TextBox1.Text;
u.u_password = TextBox2.Text;
string m="";
if (!u.Login(out m))
{
lbl_role.Text = "error";
}
else
{
if (u.u_role == "admin")
{
Response.Redirect("testclient.aspx");
}
else Response.Redirect("testserver.aspx");
}
}
执行该任务的数据库过程是:
create procedure user_proc
(@u_name nvarchar(50) ,
@u_password nvarchar(50),
@u_role nvarchar(50))
as
begin
begin try
begin transaction
if exists (select u_role from user_sys
where u_name=@u_name and u_password= @u_password)
commit
End try
Begin catch
rollback
declare @msg varchar(200)
set @msg = ERROR_MESSAGE()
raiserror(@msg , 16 , 1)
End catch
End
【问题讨论】:
标签: c# asp.net sql-server web ado.net