473,503 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code to retrieve binary field from Sql Server

I have some code to read the contents of a varbinary(max) field in SQL Server
2005. It is working ok I just wanted to know if anyone had any comments on
it's efficiency.

Code to read from the field

using (SqlDataReader reader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess ))
{
if (reader.Read())
{
MemoryStream stream = new MemoryStream();
int bufferSize = 32768;
byte[] outbyte = new byte[bufferSize];
long startIndex = 0;
int columnIndex = 4;
int bufferIndex = 0;
int bytesReceived;
BinaryWriter bw = new BinaryWriter(stream);
while ((bytesReceived =
Convert.ToInt32(reader.GetBytes(columnIndex, startIndex, outbyte,
bufferIndex, bufferSize))) 0)
{
bw.Write(outbyte, 0, bytesReceived);
bw.Flush();
startIndex += bufferSize;
}

cv.CVContent = stream;

return cv;

}

*********************************************

Code to write into the field

using (SqlCommand cmd = new SqlCommand())
{
int bufferSize = 32768;
Byte[] binaryDataBuffer = new Byte[bufferSize];
Byte[] binaryData = new Byte[inputStream.Length];
int bytesRead;

BinaryReader br = new BinaryReader(inputStream);
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);

while ((bytesRead = br.Read(binaryDataBuffer, 0,
bufferSize)) 0)
{
bw.Write(binaryDataBuffer, 0, bytesRead);
}

binaryData = ms.ToArray();

cmd.Connection = gwenv.DatabaseConnection;
cmd.Parameters.Add("CVContent", SqlDbType.VarBinary).Value =
binaryData;
--
Scott
Mar 23 '07 #1
1 20205
You can use the

SqlBytes bytes = reader.GetSqlBytes(1);

method, which vastly cuts down on the amount of code.

Then, for example:
Bitmap productImage = new Bitmap(bytes.Stream);

Here is the documentation page:

http://msdn2.microsoft.com/en-us/library/a1904w6t.aspx

Peter


--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"scottrm" wrote:
I have some code to read the contents of a varbinary(max) field in SQL Server
2005. It is working ok I just wanted to know if anyone had any comments on
it's efficiency.

Code to read from the field

using (SqlDataReader reader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess ))
{
if (reader.Read())
{
MemoryStream stream = new MemoryStream();
int bufferSize = 32768;
byte[] outbyte = new byte[bufferSize];
long startIndex = 0;
int columnIndex = 4;
int bufferIndex = 0;
int bytesReceived;
BinaryWriter bw = new BinaryWriter(stream);
while ((bytesReceived =
Convert.ToInt32(reader.GetBytes(columnIndex, startIndex, outbyte,
bufferIndex, bufferSize))) 0)
{
bw.Write(outbyte, 0, bytesReceived);
bw.Flush();
startIndex += bufferSize;
}

cv.CVContent = stream;

return cv;

}

*********************************************

Code to write into the field

using (SqlCommand cmd = new SqlCommand())
{
int bufferSize = 32768;
Byte[] binaryDataBuffer = new Byte[bufferSize];
Byte[] binaryData = new Byte[inputStream.Length];
int bytesRead;

BinaryReader br = new BinaryReader(inputStream);
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);

while ((bytesRead = br.Read(binaryDataBuffer, 0,
bufferSize)) 0)
{
bw.Write(binaryDataBuffer, 0, bytesRead);
}

binaryData = ms.ToArray();

cmd.Connection = gwenv.DatabaseConnection;
cmd.Parameters.Add("CVContent", SqlDbType.VarBinary).Value =
binaryData;
--
Scott
Mar 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1216
by: dw | last post by:
I want to create a databound dropdown list with "Yes" = 1 and "No" = 0. I set the collection up with the two values and databind it to a binary field (sql server) and the only entry in dropdown...
0
2465
by: Channing Jones | last post by:
Hello everyone, I am trying to store data in a binary field of an SQL-Server table using ADODB. So far, I have managed to store a record but not any data in the binary field. I only get...
2
2507
by: John H | last post by:
Hi , Have a Visual C++ app that use odbc to access sql server database. Doing a select to get value of binary field and bind a char to that field as follows , field in database in binary(16) ...
2
2108
by: deko | last post by:
I'm wondering if anyone has come across something like this before and could offer some suggestions. I need to extract all the Single values packed in a Long Binary field (4k chunks) and then...
0
1421
by: S Wheeler | last post by:
Hi All - I am creating a upgrade utility that transfers an bin / exe image over an xml stream. But I can not seem to get the deserialization of the binary field to work correctly. What I have is...
1
1229
by: shaktishakya | last post by:
why does mssql encrypt or convert the values (entered through sql query into a binary field) into hexadecimal values. When I query and print them from php, it displays some unicode characters. How...
1
3751
by: Asking | last post by:
Hi all, how to save/read content of a RichTextBox to/from binary field in Ms.SQL Server ? I want to do save and read operation... I couldn't it Thank you
1
1550
by: John Wright | last post by:
I have a device hooked up to my COM port that I read data from the serial port into my program. If the device is in its double mode the information that comes back is an 8 bit binary field for...
1
1967
by: boss1 | last post by:
hi all, i m new in php.i have a problem that is i m using a php form where i can retrieve one field value from one table in oracle db by ajax and its working properly. but i need to insert the...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.