如何在 C# WCF 中返回 SqlDataReader? [英] How to return SqlDataReader in C# WCF?

查看:25
本文介绍了如何在 C# WCF 中返回 SqlDataReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 C# WCF 服务中,我有一个 SqlDataReader 保存我想返回给客户端的数据行.

In my C# WCF service I have a SqlDataReader that holds rows of data that I would like to return to the client.

如何返回SqlDataReader中的所有内容?

How to return everything in the SqlDataReader?

现在我有

if (sqlReader != null)
{
    if (sqlReader.HasRows)
    {
        while (sqlReader.Read())
        {
            return sqlReader[0].ToString();
        }
        sqlConn.Close();
    }
    else
    {
        return null;
    }
}

那只返回第一个结果.目前该类的返回类型是字符串.我在想像 array in array 这样的东西,但我不知道如何?

That only returns the first result. The return type of the class is string at the moment. I was thinking of something like array in array, but I am not sure how?

感谢您的众多回复.我有兴趣返回服务创建"的整个 SQL 数据.未在线第一列 ([0]) - 这仅用于测试.

Thanks for the many replies. I am interested in returning the entire SQL data that the service 'creates'. Not online the first column ([0]) - this was only for testing.

但我不确定如何将服务中的所有内容返回给客户端.

But I am not sure on how to get everything from the service back to the client.

  • 如何退货?

例如.在 Powershell 中,如果我必须在客户端之间传递它,我会创建一个集合并将对象添加到该集合中.

For eg. in Powershell I would create a collection and add objects to that collection, if I had to pass it between clients.

我正在寻找类似 C# 和 WCF 的东西.

I am looking for something similar in C# and WCF.

到目前为止非常感谢:)

Many thanks so far :)

编辑#2:

明白了!:)

创建一个新类(例如):

Created a new class (e.g.):

public class ObjectNotification
{
    public string AlertDescription;
    public string Servername; 
}

在我的 svc.cs 文件中:

In my svc.cs file in top:

List<ObjectNotification> objlist = new List<ObjectNotification>();

    if (sqlReader.HasRows)
    {
       while (sqlReader.Read())
       {
             ObjectNotification obj = new ObjectNotification();
             obj.AlertDescription = sqlReader["AlertDescription"].ToString();
             obj.Servername = sqlReader["ComputerName"].ToString();
             objlist.Add(obj);
       }
    }
    return objlist;

这正是我想要的:)

最好的问候

推荐答案

// for instance
List<string> list = new List<string>();

if (sqlReader != null)
    {
        if (sqlReader.HasRows)
        {
            while (sqlReader.Read())
            {
                //return sqlReader[0].ToString();
                list.Add(sqlReader[0].ToString());
            }
            sqlConn.Close();
        }
        else
        {
            return null;
        }
    }
return list; // ta-da

这篇关于如何在 C# WCF 中返回 SqlDataReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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