473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

object to a byte[]: revisit

Hello All,

While replying to my post many lost the track of what I was asking but it was nice to know few extra things. Well coming back to my problem --

My field is varbinary in SQL db. Do you think converting it into ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColName"];

also

how can I save a byte[] to database?
Thank you,
Po
Jan 11 '06 #1
5 2207
Pohihihi <no*****@hotmail.com> wrote:
While replying to my post many lost the track of what I was asking
but it was nice to know few extra things. Well coming back to my
problem --

My field is varbinary in SQL db. Do you think converting it into ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColName"];

also

how can I save a byte[] to database?


Well, have you tried casting row["SomeColName"] to a byte array? I
don't know offhand if that'll work, but it's worth a try. If it
doesn't, look in the debugger at what row["SomeColName"] actually
returns - what type of object.

As for saving - I would expect that once you've worked out how to get
it out, you could reverse the process to put it back in, the same as
any other type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #2
I guess I miss the part of casting as array (?)

you mean

byte[] b = (byte[])row["SomeColName"];

never did this before but surely will try if this works, until now I was
only doing (byte) and never tried (byte[]). For the return type I am
positive that it will return byte[] or DBNull in case empty col.

Thank you,
Po


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pohihihi <no*****@hotmail.com> wrote:
While replying to my post many lost the track of what I was asking
but it was nice to know few extra things. Well coming back to my
problem --

My field is varbinary in SQL db. Do you think converting it into
ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColName"];

also

how can I save a byte[] to database?


Well, have you tried casting row["SomeColName"] to a byte array? I
don't know offhand if that'll work, but it's worth a try. If it
doesn't, look in the debugger at what row["SomeColName"] actually
returns - what type of object.

As for saving - I would expect that once you've worked out how to get
it out, you could reverse the process to put it back in, the same as
any other type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 11 '06 #3
Pohihihi <no*****@hotmail.com> wrote:
I guess I miss the part of casting as array (?)

you mean

byte[] b = (byte[])row["SomeColName"];
Yup.
never did this before but surely will try if this works, until now I was
only doing (byte) and never tried (byte[]). For the return type I am
positive that it will return byte[] or DBNull in case empty col.


Right. Casting to byte certainly wouldn't work.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #4
KJ
Responding to the latter question:

byte[] b = new byte[100];
SqlParameter sp = new SqlParameter("p", SqlDbType.VarBinary, 100);
sp.Value = b;

Jan 11 '06 #5
Hi,

Find below the code to store an binary value (in this case a file) in the DB.
Also you see how I get a file back from the DB with disregard of its type.

//To save:
SqlCommand com = new SqlCommand();
com.CommandText = "SaveDocument";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@OrigName", SqlDbType.VarChar).Value = Path.GetFileName(physicalname);
SqlParameter param = com.Parameters.Add("@data", SqlDbType.Image);
FileStream file = new FileStream( physicalname, FileMode.Open);
byte[] buff = new byte [ file.Length];
file.Read(buff, 0, Convert.ToInt32(file.Length));
file.Close();
param.Value = buff;
id = Convert.ToInt32( DataProvider.ExecuteScalar( com) );

//To retrieve:
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "LoadDocument";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dID", SqlDbType.Int).Value = id;
SqlDataReader reader = DataProvider.ExecuteReader( cmd);
this.name = Guid.NewGuid().ToString() + "." + Path.GetExtension( reader["OrigName"].ToString() );
physicalname = path + @"\" + name;
FileStream file = new FileStream( physicalname, FileMode.Create);
file.Write( (byte[])reader["Data"], 0, ((byte[])reader["Data"]).GetUpperBound(0)+1 );
file.Close();


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Pohihihi" <no*****@hotmail.com> wrote in message news:um**************@TK2MSFTNGP10.phx.gbl...
Hello All,

While replying to my post many lost the track of what I was asking but it was nice to know few extra things. Well coming back to my problem --

My field is varbinary in SQL db. Do you think converting it into ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColName"];

also

how can I save a byte[] to database?
Thank you,
Po
Jan 11 '06 #6

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

Similar topics

15
by: Lou | last post by:
How do I get the data in the clipbaord fro a registered data type. The code snippet below doesn't work? But the VB6 example does. What am I doing wrong. private void lstItems_DragDrop(object...
4
by: Moe Sizlak | last post by:
Hi There, I have a user control that has 2 listmenus populated from a database, I want the form to submit when the listmenu are changed which seems to happen no problem when the page is on it's...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
4
by: DazedAndConfused | last post by:
I encryted a serialized binary formatted object. Now I can't figure out how to deserialize it so that I can decrypt it. I used this code encrypt and write it out: Dim fe As New...
1
by: DazedAndConfused | last post by:
Can you encrpt a serialized object? Or am I trying to do something that just doesn't work that way? I am trying to encrypt a serialized object. I can read and write the object to a file...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
4
by: Goh | last post by:
Hi, I would like to know how can we implement a web page that intelligent enough to unique identify that pc have been visit before without any cookies and login user require. I have try...
5
by: JoeC | last post by:
I am still working in my maze game and I am making improvments. The main sticking problem that I have is passing my graphic library. This probram uses windows apis but my question has nothing to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

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.