gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:57 25 4
gpt4 key购买 nike

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

这是表格

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# 中,到目前为止这似乎行不通?

最佳答案

我会把这个程序写得有点不同,像这样......

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();
}

关于c# - 从 SQL Server 存储过程返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31456658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com