C#编写windows应用程序

来源:百度知道 编辑:UC知道 时间:2024/05/14 18:16:33
*****急急*****
SqlConnection con = new SqlConnection("server=.;database=BookManage;uid=sa;pwd=;");
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from Master where username='" + txtUsername + "'and password='" + txtPassword + "'and department='" + cmbDepartment + "'", con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{//}
else
{//}
在数据库中存在输入的数据,可是运行后count的值为0。怎么回事?
改为:
int count = Convert.ToInt32(cmd.Executenonqury()); 还是原来的问题。不好使。。。
可以从里边取出数据,就是一直返回0。无耐了。。

你确认你的条件能查到数据

把select count(*) from Master where username='" + txtUsername + "'and password='" + txtPassword + "'and department='" + cmbDepartment + "'"

这句话print出来放到数据库执行一下看看

//--------------------------------------
这个看不出什么端倪了

你调试一下,单步走走看,ExecuteScalar()返回值为0比较奇怪

你先把你的
SqlCommand cmd = new SqlCommand("select count(*) from Master where username='" + txtUsername + "'and password='" + txtPassword + "'and department='" + cmbDepartment + "'", con);
该成
SqlCommand cmd = new SqlCommand("select * //就只改了这个地方from Master where username='" + txtUsername + "'and password='" + txtPassword + "'and department='" + cmbDepartment + "'", con);
先看看能不能取出东西多 如果不能 就是查询字符串写错了 如果取不出 当然是0
只有这两种可能