0

我是存储过程世界的新手。

我们有现有的系统和现有的存储过程,它检查用户名和 url 路径。存储过程将检查用户详细信息是否存在。如果存在,则返回用户名所属的组名。

我想要的只是为这个存储过程编写 asp.net c# 代码。传递用户详细信息和路径并将存储过程返回值返回到字符串变量中。

下面的 SQL Server 执行存储过程的链接

http://www.natboxservices.com/helmshore/img/pic.jpg

SQL Server 输出值的链接如下所示

http://www.natboxservices.com/helmshore/img/2012-07-03%2009.35.13.jpg

请注意,上面的图片组名是空的,因为我执行了输入错误的存储过程,并且在 sqlserver 中不存在该数据的组名。如果存在,它将返回组名的值,例如“管理员”或“经理”

我的问题是“如果数据存在并返回组名我想将该值传递给字符串变量”用于分配参数并将返回值传递给字符串变量

这是我的存储过程

USE [IRC_siteadmin]
GO
/****** Object:  StoredProcedure [dbo].[sp_intranet_GetSecurity]    Script Date: 07/03/2012 10:05:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_intranet_GetSecurity]

@username VARCHAR(50),
@path VARCHAR(200),
@errorID INT OUTPUT

AS

BEGIN

SELECT
    G.name as groupname
FROM
    tbl_site_link L WITH(NOLOCK)
INNER JOIN
    tbl_site_link_group LG WITH(NOLOCK)
ON
    L.link_id = LG.link_id
INNER JOIN
    tbl_site_group G WITH(NOLOCK)
ON
    LG.group_id = G.group_id
INNER JOIN
    (       SELECT 
                group_id
            FROM 
                tbl_site_user U WITH(NOLOCK) 
            INNER JOIN
                tbl_site_user_group UG WITH(NOLOCK)
            ON
                U.user_id = UG.user_id
            WHERE 
                [name] = @Username ) U
ON
    G.group_id = U.group_id
WHERE
    L.url LIKE '%'+@path+'%'
    AND
    (U.group_id IS NOT NULL)

UNION

SELECT
    'Uber' as groupname
FROM
    tbl_site_link L WITH(NOLOCK)
INNER JOIN
    tbl_site_link_group LG WITH(NOLOCK)
ON
    L.link_id = LG.link_id
CROSS JOIN
    (       SELECT 
                group_id
            FROM 
                tbl_site_user U WITH(NOLOCK) 
            INNER JOIN
                tbl_site_user_group UG WITH(NOLOCK)
            ON
                U.user_id = UG.user_id
            WHERE 
                [name] = @Username
            AND
                group_id = 1 ) U
WHERE
    L.url LIKE '%'+@path+'%'
    AND
    (U.group_id IS NOT NULL)

END

任何帮助表示赞赏

谢谢

4

2 回答 2

1

如何:执行返回单个值的存储过程

从 C# 中的存储过程中获取返回值

于 2012-07-03T09:04:06.917 回答
0

尝试使用此代码

             string stringConnection = "Your connection to your database";
            using(var connection = new SqlConnection(stringConnection)
            {
                connection.Open();

                using (var command = new SqlCommand("sp_intranet_GetSecurity", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar));
                    command.Parameters["@username"].Value = "Your value";

                    command.Parameters.Add(new SqlParameter("@path", SqlDbType.VarChar, 8));
                    command.Parameters["@path"].Value = "Your value";

                    command.Parameters.Add(new SqlParameter("@errorID", SqlDbType.VarChar, 8));
                    command.Parameters["@errorID"].Value = "Your value";
                    command.Parameters["@errorID"].Direction = ParameterDirection.Output;

                    // Objet DataReader
                    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    Object[] row = null;
                    while (reader.Read())
                    {
                        if (row == null)
                        {
                            row = new Object[reader.FieldCount];
                        }
                        reader.GetValues(row);
                        for (int i = 0; i < row.GetLength(0); i++)
                        {
                            if (row[i] != DBNull.Value)
                            {
                                Console.Write(row[i]);
                            }
                            else
                            {
                                Console.Write("NULL");
                            }
                            if (i < row.GetUpperBound(0))
                            {
                                Console.Write("|");
                            }
                        }
                    }


               }
            }
于 2012-07-03T09:23:19.700 回答