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.ExecuteRead er(CommandBehav ior.SequentialA ccess))
{
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(st ream);
while ((bytesReceived =
Convert.ToInt32 (reader.GetByte s(columnIndex, startIndex, outbyte,
bufferIndex, bufferSize))) 0)
{
bw.Write(outbyt e, 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[] binaryDataBuffe r = new Byte[bufferSize];
Byte[] binaryData = new Byte[inputStream.Len gth];
int bytesRead;
BinaryReader br = new BinaryReader(in putStream);
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms );
while ((bytesRead = br.Read(binaryD ataBuffer, 0,
bufferSize)) 0)
{
bw.Write(binary DataBuffer, 0, bytesRead);
}
binaryData = ms.ToArray();
cmd.Connection = gwenv.DatabaseC onnection;
cmd.Parameters. Add("CVContent" , SqlDbType.VarBi nary).Value =
binaryData;
--
Scott 1 20219
You can use the
SqlBytes bytes = reader.GetSqlBy tes(1);
method, which vastly cuts down on the amount of code.
Then, for example:
Bitmap productImage = new Bitmap(bytes.St ream);
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.ExecuteRead er(CommandBehav ior.SequentialA ccess))
{
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(st ream);
while ((bytesReceived =
Convert.ToInt32 (reader.GetByte s(columnIndex, startIndex, outbyte,
bufferIndex, bufferSize))) 0)
{
bw.Write(outbyt e, 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[] binaryDataBuffe r = new Byte[bufferSize];
Byte[] binaryData = new Byte[inputStream.Len gth];
int bytesRead;
BinaryReader br = new BinaryReader(in putStream);
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms );
while ((bytesRead = br.Read(binaryD ataBuffer, 0,
bufferSize)) 0)
{
bw.Write(binary DataBuffer, 0, bytesRead);
}
binaryData = ms.ToArray();
cmd.Connection = gwenv.DatabaseC onnection;
cmd.Parameters. Add("CVContent" , SqlDbType.VarBi nary).Value =
binaryData;
--
Scott
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 is "True" or "False"
depending on the value in the field.
What am I doing wrong?
Thanks,
|
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 exceptions on various part. I have tried both AppendChunk and
using a stream object.
Unfortunately all the examples I can find in the Net and on the...
|
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)
char lpResourceID;
rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, &lpResourceID,
RESOURCE_ID_LEN_PLUS_NULL , &nLen1);
and this works fine , however trying...
|
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 export to Excel.
The challenge is arranging the values in columns and rows so they can be
exported and land in Excel properly for charting. The way...
|
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 a base 64 encoded field but when I
try to deserialize, I throw an XML / reflection exception. However, if I
remove the base64 (m_Data) stuff...
| |
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 do I solve this?
|
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
|
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 each record on the machine.
Set up as follows
Bit 7 -- Data type (Set to a 1)
Bit 6 -- Not used (set to 0)
Bits 5 - 0 -- Set to a 1 if the...
|
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 retrieved field value into another table from the same form.i m using session but not working.
how can i do this?can anyone please solve it for me.
...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
| |