how to convert a object to a byte array byte[]
-
HI My problem is that I am reading a image field from the database table . then I need to Convert that image field value into a byte[] . dread = sqlcomm.ExecuteReader(); sqlcomm = new SqlCommand("getCont1" ,sqlconn); sqlcomm.CommandType = CommandType.StoredProcedure; dread = sqlcomm.ExecuteReader(); while ( dread.Read()) { temp= (dread.GetValue(0)); } this "temp" var store the image field value as object . now i need to convert this object into byte[] as i have to pass it to a function which take byte[] as parameter. what I need to do ? thanks ss thanks
-
HI My problem is that I am reading a image field from the database table . then I need to Convert that image field value into a byte[] . dread = sqlcomm.ExecuteReader(); sqlcomm = new SqlCommand("getCont1" ,sqlconn); sqlcomm.CommandType = CommandType.StoredProcedure; dread = sqlcomm.ExecuteReader(); while ( dread.Read()) { temp= (dread.GetValue(0)); } this "temp" var store the image field value as object . now i need to convert this object into byte[] as i have to pass it to a function which take byte[] as parameter. what I need to do ? thanks ss thanks
An
image
SQL server column type translates to a byte array (byte[]
)framework type. In your example, the 'temp' variable would have to be declared asbyte[]
. You could also use theGetBytes(int index)
method on your reader. For a more complete type mapping table, look here[^].The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
-
HI My problem is that I am reading a image field from the database table . then I need to Convert that image field value into a byte[] . dread = sqlcomm.ExecuteReader(); sqlcomm = new SqlCommand("getCont1" ,sqlconn); sqlcomm.CommandType = CommandType.StoredProcedure; dread = sqlcomm.ExecuteReader(); while ( dread.Read()) { temp= (dread.GetValue(0)); } this "temp" var store the image field value as object . now i need to convert this object into byte[] as i have to pass it to a function which take byte[] as parameter. what I need to do ? thanks ss thanks
if you want to convert it to byte array you have to serialize it. using System.Runtime.Serialization.Formatters.Binary; /*Note i suppose your object is serializable if not add [Serializable] attrubite to your object class*/ BinaryFormatter Serialize=new BinaryFormatter(); MemoryStream stream=new MemoryStream(); byte[]ObjectInBytes=Serialize.Serialize(stream,temp); //to Deserialize stream=new MemoryStream(ObjectInBytes); stream.Seek(0,SeekOrigin.Begin); temp=(TypeOfYourObject) Serialize.Deserialize(stream); marcoryos