Click here to Skip to main content
15,896,382 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 8-Nov-15 3:36am
v4
Comments
ZurdoDev 6-Nov-15 15:42pm    
I suggest you return that much data on flash drive. ;)
Suvendu Shekhar Giri 6-Nov-15 15:59pm    
:D
Glagace_7763 6-Nov-15 16:40pm    
funny not helpful but funny
ZurdoDev 6-Nov-15 18:46pm    
Agreed.
George Jonsson 6-Nov-15 20:54pm    
To get better help you should provide more information.
What is the code you have done so far (relevant to the problem, of course), what error do you get and where does it occur.

1 solution

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

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.
 
Share this answer
 
Comments
Glagace_7763 8-Nov-15 13:58pm    
Thank you, you have clarified my question, the SqlDataReader is a streaming object, it will pass a single row at a time, my question was how to I convert the sqldatareader output to a stream type that I can pass from the web service to a client.
Richard MacCutchan 9-Nov-15 4:28am    
You cannot convert it. You will most likely need to create your own class based on the Stream class, that reads the reader and passes the data on to who or what is reading the stream.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900