我正在尝试根据字符串字段username从单个表中检索整数值。我已经尝试使用存储过程和直接文本。当我执行存储过程时,我得到正确的返回值;但是,没有得到正确的结果。
以下是两组代码 - 直接文字 -
public int GetUserRole(string CUSER)
{
try
{
SQLCON = new SqlConnection(connectionString);
SQLCON.Open();
SQLCommand = new SqlCommand();
SQLCommand.CommandType = CommandType.Text;
SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = CUSER";
Int32 USRole = (Int32) SQLCommand.ExecuteScalar();
return USRole;
}
catch
{
HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
return 0;
}
}
SQL查询:
ALTER PROCEDURE [dbo].[spGetUserRole]
-- Add the parameters for the stored procedure here
@username VARCHAR(50)
AS
BEGIN
-- Declare the return variable here
DECLARE @USRole as int
-- Add the T-SQL statements to compute the return value here
SELECT @USRole = tblUser.USRole FROM tblUser WHERE USUsername = @username
-- Return the result of the function
RETURN @USRole
END
答案 0 :(得分:11)
您没有正确引用参数。如果要添加名为USUsername的参数,则在命令文本中应使用@USUsername:
public int GetUserRole(string CUSER)
{
try
{
SQLCON = new SqlConnection(connectionString);
SQLCON.Open();
SQLCommand = new SqlCommand();
SQLCommand.CommandType = CommandType.Text;
SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = @USUsername";
Int32 USRole = (Int32) SQLCommand.ExecuteScalar();
return USRole;
}
catch (Exception)
{
HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
return 0;
}
}
您的存储过程也需要更新,因为此处的参数名称也应匹配,您不需要返回变量。
ALTER PROCEDURE [dbo].[spGetUserRole]
-- Add the parameters for the stored procedure here
@USUsername VARCHAR(50)
AS
BEGIN
-- Add the T-SQL statements to compute the return value here
SELECT tblUser.USRole FROM tblUser WHERE USUsername = @USUsername
END
您还应该使用“using”语法来自动关闭数据库连接。请参阅Scott Hanselman的示例 - http://www.hanselman.com/blog/WhyTheUsingStatementIsBetterThanASharpStickInTheEyeAndASqlConnectionRefactoringExample.aspx
答案 1 :(得分:1)
不使用存储过程的返回值(RETURN @USRole
),而是使用Select语句(例如Select @USRole
)发回结果。发生的事情是存储过程的返回值与ExecuteScalar使用的返回值不同。 ExecuteScalar返回输出的第一列和第一行。返回值不同,必须使用特别命名的参数@RETURN_VALUE
或特殊ParameterDirection.ReturnValue
属性进行访问。
您的程序的修订版本如下:
ALTER PROCEDURE [dbo].[spGetUserRole]
-- Add the parameters for the stored procedure here
@USUsername VARCHAR(50)
AS
BEGIN
-- Add the T-SQL statements to compute the return value here
Select tblUser.USRole
FROM tblUser
WHERE USUsername = @USUsername
END
答案 2 :(得分:1)
我不知道您是如何调用存储过程的,但是您发布的查询中存在错误:
"SELECT USRole FROM tblUser WHERE USUsername = CUSER"
应替换为
SQLCommand.Parameters.Add("@USUsername", SqlDbType.VarChar).Value = CUSER;
"SELECT USRole FROM tblUser WHERE USUsername = @USUsername"
您目前没有真正使参数成为查询的一部分,而是尝试在列
答案 3 :(得分:0)
正确使用参数。并且不要忘记在finally语句中关闭连接。
public int GetUserRole(string CUSER)
{
try
{
SQLCON = new SqlConnection(connectionString);
SQLCON.Open();
SQLCommand = new SqlCommand();
SQLCommand.CommandType = CommandType.Text;
SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = @USUsername ";
SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
Int32 USRole = (Int32) SQLCommand.ExecuteScalar();
return USRole;
}
catch (Exception)
{
HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
return 0;
}
finally { close connection here.. }
}
答案 4 :(得分:0)
如果您坚持使用返回值,可以通过设置参数方向并使用ExecuteNonQuery
来实现SqlParameter p = cmd.Parameters.Add("@USRole", SqlDbType.Int);
p.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
int returnvalue = (int)cmd.Parameters["@USRole"].Value;
如果你想使用ExecuteScalar,那么只需将你的proc改为select
变量而不是Return
您应该注意,您为参数名称添加的内容是任意的。