object to a byte[]: revisit

P

Pohihihi

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
 
J

Jon Skeet [C# MVP]

Pohihihi said:
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.
 
P

Pohihihi

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 said:
Pohihihi said:
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.
 
J

Jon Skeet [C# MVP]

Pohihihi said:
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.
 
K

KJ

Responding to the latter question:

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

Ignacio Machin \( .NET/ C# MVP \)

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

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
 

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