如何阅读SQL函数值由SQL数据读取器在C#

编程入门 行业动态 更新时间:2024-10-23 23:22:20
本文介绍了如何阅读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#

本文发布于:2023-10-26 09:39:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   读取器   数据   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!