471,306 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 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 2120
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Lou | last post: by
3 posts views Thread by Adriano | last post: by
1 post views Thread by DazedAndConfused | last post: by
7 posts views Thread by Martin Robins | last post: by
5 posts views Thread by JoeC | last post: by
reply views Thread by rosydwin | last post: by

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.