从SQL Server存储过程返回多个值

时间:2015-07-16 14:10:33

标签: c# sql-server stored-procedures

我想要做的是使用我的spLogin存储过程从同一个表中返回两个值,我想在C#中的会话中保存这两个值。

这是表格

create table tbClients
(
    ClientID int primary key identity(0,1),
    ClientFirstName varchar(20),
    ClientLastName varchar(20),
    ClientAddress varchar(60),
    ClientOrigin varchar(20),
    ClientUsername varchar(20),
    ClientPassword int,
    ClientSecurity int
)

当客户端点击登录按钮时,我想编写一个程序来检查用户是否有效,他们的安全级别是什么,以及他们的名字是什么。

这是我到目前为止所拥有的

create procedure spLogin(
    @ClientUsername varchar(20),
    @ClientPassword int
)
AS BEGIN
    DECLARE @Security int
    DECLARE @ClientFirstName varchar(20)        

    IF EXISTS (SELECT * FROM tbClients 
               WHERE ClientUsername = @ClientUsername 
                 AND ClientPassword = @ClientPassword)
    BEGIN               
        SELECT 
            @Security = ClientSecurity, 
            @ClientFirstName = ClientFirstName 
        FROM tbClients 
        WHERE
            ClientUsername = @ClientUsername 
            AND ClientPassword = @ClientPassword

        IF(@Security = 1)
        BEGIN
            SELECT 'Admin' as Security, @ClientFirstName
        END
        ELSE
        BEGIN
            SELECT 'Customer' as Security, @ClientFirstName
        END
    END
    ELSE
    BEGIN
        SELECT 'INVALID'
    END
END
GO

不知道这是否有效,因为我不知道如何在不使用数据集的情况下将这些值存储在C#中,到目前为止似乎无法正常工作?

4 个答案:

答案 0 :(得分:6)

我会写这个程序有点像这样......

create procedure spLogin
    @ClientUsername     varchar(20)
   ,@ClientPassword     int
   ,@Security           VARCHAR(10)     OUTPUT
   ,@ClientFirstName    varchar(20)     OUTPUT
   ,@ValidLogin         INT             OUTPUT
AS 
BEGIN
  SET NOCOUNT ON;        

    IF EXISTS (SELECT * FROM tbClients 
               WHERE ClientUsername = @ClientUsername 
                 AND ClientPassword = @ClientPassword)
      BEGIN               
            SELECT @ValidLogin = 1 
                  ,@Security = CASE WHEN ClientSecurity = 1 
                                 THEN 'Admin' ELSE 'Customer' END
                  ,@ClientFirstName = ClientFirstName 
            FROM tbClients 
            WHERE ClientUsername = @ClientUsername 
              AND ClientPassword = @ClientPassword
      END
    ELSE
      BEGIN
            SET @ValidLogin = 0;
      END
END
GO

不是C#的专家,但你会在C#中处理输出参数,比如....

// define connection and command, in using blocks to ensure disposal
using(SqlConnection conn = new SqlConnection(pvConnectionString ))
using(SqlCommand cmd = new SqlCommand("dbo.spLogin", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;

    // set up the input parameters
    cmd.Parameters.Add("@ClientUsername", SqlDbType.VarChar, 20);
    cmd.Parameters.Add("@ClientPassword", SqlDbType.Int);
    cmd.Parameters.Add("@Security", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@ClientFirstName", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@Success", SqlDbType.Int).Direction = ParameterDirection.Output;

    // set parameter values
    cmd.Parameters["@ClientUsername"].Value = UserNamTextbox.Text;

    // open connection and execute stored procedure
    conn.Open();
    cmd.ExecuteNonQuery();

    // read output value from @Security
    int Security = Convert.ToInt32(cmd.Parameters["@Security"].Value);

    if Security == 1 ....... and so on.......

    conn.Close();
}

答案 1 :(得分:0)

所以我不是100%确定你有多少C#,但这里是基础知识:

使用此SO问题中的代码从SQL Server获取数据: Fill DataTable from SQL Server database

然后,一旦你拥有了数据表,就可以使用for循环遍历它(假设你将Datatable定义为'dt'):

foreach (DataRow row in dt.Rows) 
{
    //do what you need with your data.
    var value = row["ColumnName"];
    Console.WriteLine(value.ToString());
}

希望有所帮助。

答案 2 :(得分:0)

商店流程只返回一行的结果,您可以检查数据表中是否存在列:

ember server

答案 3 :(得分:0)

您可以执行storedprocedure并将结果填充到数据集中。

每个select语句都将存储为数据表,您可以使用索引访问相应的表。