akshaykumar6
akshaykumar6

Reputation: 2257

Get byte[] from the SqlDataReader?

I have a table with a varbinary(MAX) parameter. After executing the query, I will store the result in SqlDataReader as it has multiple columns as output.

SqlCommand cmd = new SqlCommand("select leave_details from LeaveTable");
SqlDataReader obj;
con.Open();
obj = cmd.ExecuteReader();

Now I want to convert the result for each row into byte[].

This one is not working:

byte[] b=null;
obj.GetBytes(0,0,b,0,1024);

Upvotes: 21

Views: 36644

Answers (2)

Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13592

string bytesfromsql = "01010101010110010111010001"; //for example
byte[] bytes = new byte[bytesfromsql.Length];
for (int i = 0; i < bytes.Count(); i++)
{
   bytes[i] = byte.Parse(bytesfromsql[i].ToString());
}

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94653

Try GetValue() method.

byte[] b=null;
b=(byte [])obj.GetValue(0);
//OR
b=(byte [])obj[0];

Upvotes: 42

Related Questions