Dealing with BLOBs

G

Guest

Hi

I'm working on a C# windows application witch retrieve BLOBs Data from
Database.
I'm doing that using SqlDataReader with SequentialAccess CommandBehavior.
My code is something like the bellow:

SqlConnection cnn = new SqlConnection(Global.ConnectionString);

SqlCommand cmd = new SqlCommand("SELECT Img FROM Imgs WHERE [item_id] =
555000000300990", cnn);

cnn.Open();

IDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
long size = dr.GetBytes(0, 0, null, 0, 0); //get the length of data
byte[] values = new byte[size];

int bufferSize = 100;
long bytesRead = 0;
int curPos = 0;

while (bytesRead < size)
{
bytesRead += dr.GetBytes(0, curPos, values, curPos, bufferSize);
curPos += bufferSize;
}

This one is working perfect, but if I change the code to read data from a
Text column in DB and use GetChars, I get an exception witch means you have
to read columns in order.

Even I remove the line # 10 (long size = dr.GetBytes(0, 0, null, 0, 0);
//get the length of data)

Why this method is working for Bytes but not for Characters?

Thanks in advance
 
M

Miha Markic [MVP C#]

You add a text field or change Img field to your text field in SELECT
statement?
 
G

Guest

Yea, I changed everything witch needed to be changed.
Like this:

SqlConnection cnn = new SqlConnection(Global.ConnectionString);

SqlCommand cmd = new SqlCommand("SELECT Txt FROM Notes WHERE [item_id] =
555000000300990", cnn);

cnn.Open();

IDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
long size = dr.GetChars(0, 0, null, 0, 0); //get the length of data
char[] values = new char[size];

int bufferSize = 100;
long bytesRead = 0;
int curPos = 0;

while (bytesRead < size)
{
bytesRead += dr.GetChars(0, curPos, values, curPos, bufferSize);
curPos += bufferSize;
}




Miha Markic said:
You add a text field or change Img field to your text field in SELECT
statement?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Amir said:
Hi

I'm working on a C# windows application witch retrieve BLOBs Data from
Database.
I'm doing that using SqlDataReader with SequentialAccess CommandBehavior.
My code is something like the bellow:

SqlConnection cnn = new SqlConnection(Global.ConnectionString);

SqlCommand cmd = new SqlCommand("SELECT Img FROM Imgs WHERE [item_id] =
555000000300990", cnn);

cnn.Open();

IDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
long size = dr.GetBytes(0, 0, null, 0, 0); //get the length of data
byte[] values = new byte[size];

int bufferSize = 100;
long bytesRead = 0;
int curPos = 0;

while (bytesRead < size)
{
bytesRead += dr.GetBytes(0, curPos, values, curPos, bufferSize);
curPos += bufferSize;
}

This one is working perfect, but if I change the code to read data from a
Text column in DB and use GetChars, I get an exception witch means you
have
to read columns in order.

Even I remove the line # 10 (long size = dr.GetBytes(0, 0, null, 0, 0);
//get the length of data)

Why this method is working for Bytes but not for Characters?

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top