Web服务流式传输SQL数据 [英] Web Service streaming sql data

查看:191
本文介绍了Web服务流式传输SQL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个将传回许多行数据的Web服务方法,我正在考虑从sqldatareader中回送数据,但是当我发送到流时,我得到的错误无法转换为io.stream。有没有办法我可以在sqldatareader周围包裹一个内存流并将其传回去?



这是一个简单的方法:



[WebMethod]

public Stream RetrieveFile()

{

SqlConnection connection = new SqlConnection(Properties.Settings.Default .ConnectionString);



SqlCommand command = new SqlCommand(select * from tblcase_casedetail,connection);



connection.Open();



SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);



reader.Read();



返回读者;





}



返回错误

无法将类型'System.Data.SqlClient.SqlDataReader'隐式转换为'System。 IO.Stream'



我不喜欢由于存在内存不足错误,我希望能够将数据行传输到客户端,将行插入到类似的数据表中。



我在VS2012和SQL Server 20108 R2中工作

I have to write a web service method that will pass back many rows of data, I was thinking about steaming the data back from a sqldatareader but when I sent up to stream I get the error cannot convert to io.stream. Is there a way I can maybe wrap a memorystream around the sqldatareader and pass that back?

Here is the simple method:

[WebMethod]
public Stream RetrieveFile()
{
SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString);

SqlCommand command = new SqlCommand("select * from tblcase_casedetail", connection);

connection.Open();

SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);

reader.Read();

return reader;


}

the error returned
Cannot implicitly convert type 'System.Data.SqlClient.SqlDataReader' to 'System.IO.Stream'

I don't want to buffer since there is a chance of a out of memory error, I want to be able to just stream rows of data to a client that will insert the rows into a similar data table.

I'm working in VS2012 and SQL Server 20108 R2

推荐答案

public Stream RetrieveFile()
{
    // ...
    SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
    reader.Read();
    
    return reader;
    }



你不能声明一个方法来说它返回一个 Stream 对象,然后尝试从中返回 SqlDataReader 。您需要将SQL查询的结果转换为流,或者调用者可以从Web服务发送的其他对象。


You cannot declare a method to say that it returns a Stream object, and then try to return a SqlDataReader from it. You need to convert the result of the SQL query into a stream, or some other object that the caller can send from the web service.


这篇关于Web服务流式传输SQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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