0

嗨,我有一个tblRegistration包含三个字段的表

Member_ID(int)  Left_Placement(Int) Right_Placement(int)

和一个功能

CREATE FUNCTION [dbo].[fnGetChildCount](
@ParentId INT
)
RETURNS INT
BEGIN
 DECLARE @ChildCount INT
 DECLARE @Left INT
DECLARE @Right INT


SET @ChildCount = 0
 IF NOT @ParentId IS NULL AND @ParentId <> 0
 BEGIN
 SET @ChildCount = 1
 SELECT @Left = Left_Placement, @Right = Right_Placement FROM tblRegistration WHERE Member_ID = @ParentId
SELECT @ChildCount = @ChildCount + [dbo].[fnGetChildCount](@Left)
 SELECT @ChildCount = @ChildCount + [dbo].[fnGetChildCount](@Right)
 END
 RETURN @ChildCount
END;

现在我使用此代码

    SqlCommand cmd = new SqlCommand("SELECT Member_ID, dbo.fnGetChildCount(Left_Placement) AS [Left Count], dbo.fnGetChildCount(Right_Placement) AS [Right Count] FROM tblRegistration", con);
    dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            string member = dr["dbo.fnGetChildCount(Left_Placement)"].ToString();

        }
    }

然后读取不是表列的左计数和右计数

我如何阅读LEFT COUNT(dbo.fnGetChildCount(Left_Placement) AS [Left Count]) AND RIGHT COUNT(dbo.fnGetChildCount(Right_Placement) AS [Right Count])

先感谢您

4

1 回答 1

0

使用列别名来检索计算的列值。

string leftCount = dr["Left Count"].ToString();
string rightCount = dr["Right Count"].ToString();
于 2013-03-25T19:28:58.480 回答