从查询中检索单个值

我正在尝试根据字符串字段用户名从单个表中检索整数值。 我已经尝试使用存储过程和直接文本。 当我执行存储过程时,我得到正确的返回值; 然而,正确的结果并没有通过。

以下是两组代码 – 直接文本 –

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 

您没有正确引用参数。 如果要添加名为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

我不知道你如何调用你的存储过程,但是你发布的查询中有一个错误:

 "SELECT USRole FROM tblUser WHERE USUsername = CUSER" 

应该换成

 SQLCommand.Parameters.Add("@USUsername", SqlDbType.VarChar).Value = CUSER; "SELECT USRole FROM tblUser WHERE USUsername = @USUsername" 

您目前没有真正使参数成为查询的一部分,而是尝试在列中查找值CUSER

而不是使用存储过程的返回值( 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 

RETURN(Transact-SQL)

SqlCommand.ExecuteScalar方法

正确使用参数。 并且不要忘记在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.. } } 

如果您坚持使用返回值,则可以通过设置参数方向和使用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

您应该注意,您为参数名称添加的内容是任意的。