如何将一个存储过程的查询结果在cs文件里显示,请高手指教
这个是cs文件SqlConnectioncon=newSqlConnection();try{intuserId=0;con.ConnectionString="serv...
这个是cs文件
SqlConnection con = new SqlConnection();
try
{
int userId = 0;
con.ConnectionString = "server=.;database=net;pwd=as;uid=sa;";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "validateLogin";
cmd.Parameters.Add("@userName", SqlDbType.VarChar);
cmd.Parameters.Add("@CryptPassword", SqlDbType.VarChar);
cmd.Parameters["@userName"].Value = txtUserName.Text.Trim();
cmd.Parameters["@CryptPassword"].Value = txtPassword.Text.Trim();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
userId = int.Parse(reader["userId"].ToString());
if (userId <= 0)
lblMessage.Text = "用户名密码错误!";
else
Response.Redirect("main.aspx");
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
finally
{
con.Close();
}
}
这个是存储过程
create proc validateLogin
@UserName varchar(16),
@CryptPassword varchar(60)
as
select userId from users where userName=@UserName and password=@CryptPassword
执行到
SqlDataReader reader = cmd.ExecuteReader();报错
存储过程本身是没有错的
代码请贴出来 展开
SqlConnection con = new SqlConnection();
try
{
int userId = 0;
con.ConnectionString = "server=.;database=net;pwd=as;uid=sa;";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "validateLogin";
cmd.Parameters.Add("@userName", SqlDbType.VarChar);
cmd.Parameters.Add("@CryptPassword", SqlDbType.VarChar);
cmd.Parameters["@userName"].Value = txtUserName.Text.Trim();
cmd.Parameters["@CryptPassword"].Value = txtPassword.Text.Trim();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
userId = int.Parse(reader["userId"].ToString());
if (userId <= 0)
lblMessage.Text = "用户名密码错误!";
else
Response.Redirect("main.aspx");
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
finally
{
con.Close();
}
}
这个是存储过程
create proc validateLogin
@UserName varchar(16),
@CryptPassword varchar(60)
as
select userId from users where userName=@UserName and password=@CryptPassword
执行到
SqlDataReader reader = cmd.ExecuteReader();报错
存储过程本身是没有错的
代码请贴出来 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询