简体   繁体   English

sql即使分配时为null也不会返回值

[英]sql not returning value if null even when assigned

Using asp.net, C#, SQL Server 2008. 使用asp.net,C#,SQL Server 2008。

Scenario: User attempts to log into web app. 场景:用户尝试登录Web应用程序。 App checks username against list to get UserID . 应用程序对照列表检查用户名以获得UserID If this user is a known user, page opens fine. 如果该用户是已知用户,则可以正常打开页面。 If user is not a known user, it SHOULD open to page with notice to contact admin. 如果该用户不是已知用户, 则应打开页面以通知与管理员联系。

I have been unable to write the SQL stored procedure so that it will return a value of 0 (or anything else for that matter) so it can be used to trigger controls. 我一直无法编写SQL存储过程,因此它将返回0值(或其他任何与此相关的值),因此可以将其用于触发控件。 I've executed the SQL as a test, and it works; 我已经执行了SQL作为测试,并且可以正常工作。 but it errors out every time on the asp side. 但是每次在asp端都会出错。

Any and all assistance is greatly appreciated! 任何帮助都将不胜感激!

SQL Server stored procedure code: SQL Server存储过程代码:

ALTER PROCEDURE [dbo].[spGetUserID]
-- Add the parameters for the stored procedure here
   (@Username as varchar(64))
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @X  INT

  SET @X = (SELECT COUNT(*) FROM Users WHERE (Username = @Username))

  If @X > 0
     RETURN 
        (SELECT UID FROM Users WHERE (UserName = @Username))
  ELSE
     RETURN 99999  --IVE ALSO TRIED USING 0 HERE. 
END

ASP.NET stored procedure call ASP.NET存储过程调用

 public int GetUserID(string Username)
 { 
       try
       {
            SQLCON = new SqlConnection(connectionString);
            SQLCON.Open();
            SQLCmd = new SqlCommand("spGetUserID", SQLCON);
            SQLCmd.CommandType = CommandType.StoredProcedure;
            SQLCmd.Parameters.Add("@username", SqlDbType.VarChar).Value = Username;
            int userID = (Int32)SQLCmd.ExecuteScalar();
            return userID;  //THIS ALWAYS JUMPS TO EXCEPTION
        }
        catch
        {
            return 0;
        }
        finally
        {
            SQLCON.Close();
        }
    }

ExecuteScalar() is meant for a query that returns one row with one column. ExecuteScalar()用于查询返回一行和一列的查询。

To get the return value of a stored procedure, use a parameter with direction ReturnValue : 要获取存储过程的返回值,请使用方向为ReturnValue的参数:

sqlcomm.Parameters.Add("@retVal", SqlDbType.Int).Direction = 
    ParameterDirection.ReturnValue;
SQLCmd.ExecuteNonQuery();
var result = (int) sqlcomm.Parameters["@retVal"].Value;

Try unwrapping your select statement. 尝试展开您的select语句。 The ExecuteScalar command retrieves the first column of the first row in the result set, or a null reference if the result set is empty. ExecuteScalar命令检索结果集中第一行的第一列,如果结果集为空,则为空引用。 Your stored proc doesn't actually return a result set. 您存储的proc实际上并不返回结果集。 Try... 尝试...

ALTER PROCEDURE [dbo].[spGetUserID]
-- Add the parameters for the stored procedure here
   (@Username as varchar(64))
AS
BEGIN
  If Exists(SELECT 1 FROM Users WHERE (Username = @Username))
     SELECT UID FROM Users WHERE (UserName = @Username)
  ELSE
     SELECT 99999
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM