Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlDataReader.GetSqlBinary vs SqlDataReader.GetSqlBytes?

Under the namespace System.Data.SqlClient, we have both SqlDataReader.GetSqlBinary and SqlDataReader.GetSqlBytes.

Both seems to give "raw data". If so, what's the difference between them?

like image 573
TwentyTwo Avatar asked Jul 21 '11 11:07

TwentyTwo


People also ask

What does SqlDataReader return?

As explained earlier, the SqlDataReader returns data via a sequential stream. To read this data, you must pull data from a table row-by-row Once a row has been read, the previous row is no longer available.

Why do we use SqlDataReader?

It is used to populate an array of objects with the column values of the current row. It is used to get the next result, when reading the results of SQL statements. It is used to read record from the SQL Server database. To create a SqlDataReader instance, we must call the ExecuteReader method of the SqlCommand object.

How to add SqlDataReader in c#?

After creating an instance of the Command object, you create a DataReader by calling Command. ExecuteReader to retrieve rows from a data source, as shown in the following example. SqlDataReader myReader = myCommand. ExecuteReader();


2 Answers

The GetSQLBytes are stored in an inside buffer for more manipulation, the Binary are just a stream that you get and use it as it is.

This two return SqlBytes and SqlBinary and by see this two types you can see the full different of them and how they store the data.

http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqlbytes.storage.aspx

http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqlbytes.aspx

like image 70
Aristos Avatar answered Oct 06 '22 00:10

Aristos


GetSqlBinary returns an SqlBinary structure:

Represents a variable-length stream of binary data to be stored in or retrieved from a database.

GetSqlBytes returns an SqlBytes class:

Represents a mutable reference type that wraps either a Buffer or a Stream.

So is seems that the difference is that GetSqlBinary gives you a lump of data as a byte array, while GetSqlBytes is similar but stores the data in a buffer which allows you to interact with the underlying data as a stream.

like image 43
mdm Avatar answered Oct 05 '22 23:10

mdm