存储过程输出值为变量

时间:2012-07-03 08:49:47

标签: c# asp.net sql-server stored-procedures ado.net

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

我们现有的系统具有现有的存储过程,它检查用户名和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

请注意,上面的图片groupname为空,因为我执行了错误输入的存储过程,并且sqlserver中没有该数据的组名。如果存在,它将返回组名称的值,例如“administrator”或“Manager”

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

这是我的storedprocedure

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

任何帮助表示赞赏

感谢

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分: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("|");
                            }
                        }
                    }


               }
            }