Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - SqlDataReader and serialization

Can an SqlDataReader be passed to a session or sent to a client?

For instance, if I retrieved some rows from a database, and want to send this data to another client machine. Can I simply do this by serializing it using json on the server and then deserializing back on the client?

like image 286
Matthew Avatar asked Dec 01 '25 06:12

Matthew


2 Answers

No, only data (no methods or functionality) can be serialized, so the data reader would be useless since you could not call methods to advance the reader, etc. The pattern would be to read all the records into a list of objects from the reader, close it, and then serialize those objects back to the client.

like image 150
Jon Raynor Avatar answered Dec 03 '25 18:12

Jon Raynor


No, any resource-related object cannot be "passed" to a client.

This wouldn't really make sense. You should "materialize" the data reader and pass the results.

I.e. you can create an SqlDataAdapter from your reader, fill a DataTable and pass the DataTable.

like image 22
Stefano Altieri Avatar answered Dec 03 '25 18:12

Stefano Altieri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!