Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting binary data using SqlDataReader

Tags:

c#

ado.net

I have a table named Blob (Id (int), Data (Image)). I need to use SqlDataReader to get that image data. Note that I dont want to Response.Binarywrite() the data to the browser. I just need that binary data as byte[] to use the same for some internal operations. Only way I can think of is getting id using SqlDataReader and the again use SqlCommand.ExecuteScalar() to get that as byte[] for a given id. Can I use just the SqlDataReader (SqlCommand.ExecuteReader) to get that image data as byte[]? Am I missing anything?

like image 266
Ashish Gupta Avatar asked Mar 20 '11 20:03

Ashish Gupta


People also ask

How do I get data from ExecuteReader?

To retrieve data using a DataReader, create an instance of the Command object, and then create a DataReader by calling Command. ExecuteReader to retrieve rows from a data source.

Is there anything faster than SqlDataReader in net?

Caching namespace. If you're doing purely data operations (as your question suggests), you could rewrite your code which is using the data to be T-SQL and run natively on SQL. This has the potential to be much faster, as you will be working with the data directly and not shifting it about.

What is the use of SqlDataReader?

The SqlDataReader is used to read a row of record at a time which is got using SqlCommand. It is read only, which means we can only read the record; it can not be edited. And also it is forward only, which means you can not go back to a previous row (record).

Does SqlDataReader need to be disposed?

You don't need the . Close() statement in either sample: it's handled by the . Dispose() call.

What is sqldatareader in SQL Server?

The ADO.NET SqlDataReader class in C# is used to read data from the SQL Server database in the most efficient manner. It reads data in the forward-only direction. It means, once it read a record, it will then read the next record, there is no way to go back and read the previous record. The SqlDataReader is connection-oriented.

How to retrieve data using a DataReader in SQL Server?

Thank you. To retrieve data using a DataReader, create an instance of the Command object, and then create a DataReader by calling Command.ExecuteReader to retrieve rows from a data source. The DataReader provides an unbuffered stream of data that allows procedural logic to efficiently process results from a data source sequentially.

What is a connection-oriented sqldatareader?

The SqlDataReader is connection-oriented. It means it requires an open or active connection to the data source while reading the data. The data is available as long as the connection with the database exists

What is the use of DataReader?

The DataReader provides an unbuffered stream of data that allows procedural logic to efficiently process results from a data source sequentially. The DataReader is a good choice when retrieving large amounts of data because the data is not cached in memory. You should always call the Close method when you have finished using the DataReader object.


1 Answers

You should be able to get it via: (byte[])reader["Data"].

Also note that the image data type is deprecated, and will be removed in a future version of SQL Server; use varbinary(max) instead.

like image 116
Andy Edinborough Avatar answered Sep 20 '22 04:09

Andy Edinborough