如何阅读SQL函数值由SQL数据读取器在C# [英] How to read a SQL Function Value By SQL Data Reader in C#

查看:141
本文介绍了如何阅读SQL函数值由SQL数据读取器在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个表 tblRegistration 其中有三个领域

Hi I have a table tblRegistration which have three fields

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;

现在我用这个code

Now I use This Code

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

        }
    }

要读左数和右边计数这是不是表列然后

To read Left Count And Right Count Which Is Not table column then

我怎样读取左COUNT(dbo.fnGetChildCount(Left_Placement)AS [左计数])和右COUNT(dbo.fnGetChildCount(Right_Placement)AS [右计数])

感谢你在前进

推荐答案

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

Use the column aliases to retrieve the computed column values.

string leftCount = dr["Left Count"].ToString();
string rightCount = dr["Right Count"].ToString();

这篇关于如何阅读SQL函数值由SQL数据读取器在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆