[英]Retrieving single value from query
我正在嘗試根據字符串字段用戶名從單個表中檢索整數值。 我已經嘗試使用存儲過程和直接文本。 當我執行存儲過程時,我得到正確的返回值; 然而,正確的結果並沒有通過。
以下是兩組代碼 - 直接文本 -
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
而不是使用存儲過程的返回值( 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
我不知道你如何調用你的存儲過程,但是你發布的查詢中有一個錯誤:
"SELECT USRole FROM tblUser WHERE USUsername = CUSER"
應該換成
SQLCommand.Parameters.Add("@USUsername", SqlDbType.VarChar).Value = CUSER;
"SELECT USRole FROM tblUser WHERE USUsername = @USUsername"
您目前沒有真正使參數成為查詢的一部分,而是嘗試在列中查找值CUSER
正確使用參數。 並且不要忘記在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
您應該注意,您為參數名稱添加的內容是任意的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.