谁帮我看看啊,这个老是返回值为-1,一调试,就叫我重启vs什么问题啊,急求帮助!!
public int ValidateAdmin(string sql, string adminID,string adminPwd)
{
int i;
try
{
this.openCon();
this.cmd = new SqlCommand(sql, this.con);
this.cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] pars = new SqlParameter[] {
new SqlParameter("@AdminID", SqlDbType.VarChar,50),
new SqlParameter("@AdminPwd", SqlDbType.VarChar,50)
};
pars[0].Value = adminID;
pars[1].Value = adminPwd;
foreach (SqlParameter parameter in pars)
{
this.cmd.Parameters.Add(parameter);
}
i = (int)this.cmd.ExecuteScalar();
}
catch
{
i = -1;
}
finally
{
this.closeCon();
}
return i;
}
public void openCon()
{
if (this.con == null)
{
this.con = new SqlConnection(this.strCon);
}
if (this.con.State.Equals(ConnectionState.Closed))
{
this.con.Open();
}
}
判断一下 this.cmd.ExecuteScalar(); 是否为NULL,再做 i = (int)this.cmd.ExecuteScalar();转换。
为什么this.cmd.ExecuteScalar();返回为空啊?
数据库查询出错了,跳入catch代码块了
我看存储过程也没错啊
USE [Shopping]
GO
/****** Object: StoredProcedure [dbo].[PRO_AdminLogin] Script Date: 05/26/2012 09:37:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PRO_AdminLogin]
@AdminID varchar = 0,
@AdminPwd varchar = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM Admin WHERE AdminID=@AdminID AND AdminPwd=@AdminPwd
END
还有没可能其它原因啊
@小柑: this.cmd = new SqlCommand(sql, this.con); 这个sql语句有没有错
@┢┦偉: 没有错 我突然发现 this.cmd.ExecuteScalar()不为空 改成下面 能获得i=1
if (this.cmd.ExecuteScalar() != null)
{
i = 1;//原来是i = (int)this.cmd.ExecuteScalar(); }
@┢┦偉:
(int)this.cmd.ExecuteScalar() 如果不为空 是不是返回也有负值的啊
@小柑: 谢谢了 总算解决这个问题了
@小柑: 不会有负值,0或>0
如果返回不为空给返回值赋值,不是把this.cmd.ExecuteScalar()的值直接返回去,因为不知道会返回什么值