sql 查询问题!返回值为什么总是为0?

来源:百度知道 编辑:UC知道 时间:2024/07/16 12:34:02
如题,代码如下:
public int Dengluyanzheng(String username, String userpwd)
{
String sql = null;
if (userpwd != "")
{
sql = "select PowerIndex from Power where PowerName = '" + username + "' and PowerKey = '" + userpwd + "'";
}
else
{
sql = "select PowerIndex from Power where PowerName = '" + username + "' and PowerKey is null";
}

try
{
this.getOpen();

odcmd = new OleDbCommand(sql, odcon);
object x = (object)odcmd.ExecuteScalar();
int i = Convert.ToInt32(x);

return 0 != i ? 1 : 2;
}
catch (Exception)
{
this.get

object x = (object)odcmd.ExecuteScalar();
这句话查询ExecuteScalar()返回的是第一行的第一列的数据影响说,如果没有,则返回的是0,有则返回的是1.
你把这件改成
int x=odcmd.ExecuteNonQuery();就OK了

检查你的username和userpwd 时候含有多余的空格
if (userpwd != "")
{
sql = "select PowerIndex from Power where PowerName = '" + username.Trim() + "' and PowerKey = '" + userpwd.Trim() + "'";
}
else
{
sql = "select PowerIndex from Power where PowerName = '" + username.Trim() + "' and PowerKey is null";
}