sql c# 菜鸟求助

来源:百度知道 编辑:UC知道 时间:2024/05/23 16:08:06
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=schoolwork_upload;Integrated Security=True");

string cmdString = "select * from stu where no = userName.Text and pass = pass.Text";

SqlCommand cmd = new SqlCommand(cmdString, con);

con.Open();

SqlDataReader dr = cmd.ExecuteReader();

怎样判断登陆是否成功,c# 怎么使用 rowcount

楼上的不失为一种好方法,但是我看你既然没有用到登陆类,那么为何不直接在查询语句中统计记录的条数呢?这样连SqlDataReader都可以不用。
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=schoolwork_upload;Integrated Security=True");

string cmdString = "select Count(*) from stu where no = userName.Text and pass = pass.Text";//查询语句中直接统计条数

SqlCommand cmd = new SqlCommand(cmdString, con);

con.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());//获取查询结果(即符合条件的记录的条数)
con.Close();
if(count==0)
{
//不能登录的代码
}
else
{
//成功登陆的代码
}

希望可以帮到你,如有任何疑问,欢迎百度联系。

SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
// 如果dr.Read() 为真,那么证明会员是存在的。
.......................
}
else
{
................
}

一楼回答的是正确的,还有一种方法,
string cmdString = "select count(*) from stu where no = userName.Text and pass = pass.Text";

SqlCommand cmd = new SqlCo