views:

85

answers:

3

Hi,

Ii have a database (sql server 2008) and I am working with c# in visual studio 2010.

I want to establish a connection with my db. I have written a code for that as follows:

    SqlConnection mySqlConnection = new SqlConnection
    ("Data Source=servername\\SQL2008; Initial Catalog=MyData;UID=MyID;PWD=mypassword;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText = "SELECT image_column FROM images_table ";

    mySqlConnection.Open();

    SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();

I do not know how to proceed further. I mean how to extract the column from the reader after executing the above statements? Also I need to go through the entire column row by row and convert each image to an IContent item. Do I have to write my own converter for this or is there any .NET class that does this already?

Heres the scenario. I have a table with like 20 columns one of which is the images_column. That column is the ONLY column that has images stored in as binary data. The remainig columns are usual integers (its size value) and strings (its name, location, etc) associated with that particular image. I hope that it is clear now.

Thanks.

A: 

I'm not sure from your question exactly what you're trying to accomplish, but to get the image out of the column as a Bitmap with your reader, you could use the following.

while (productsSqlDataReader.Read())
{
    byte[] buf = productsSqlDataReader["image_column"] as byte[];

    using (MemoryStream ms = new MemoryStream(buf))
    {
       Bitmap bmp = Bitmap.FromStream(ms);
       //do whatever with the bitmap BEFORE you dispose the stream
    }
}
Steve Danner
Thanks. I planned to use the image and convert it to an IContent for my web application. IContent is a Telerik.Cms.Engine (content management system) class used as a common interface for various web functionalities like blogs,news,etc. By converting it to an IContent item I can do some more things with it later.
VP
Perhaps there's a way to create an IContent class from a byte array? BTW, you will need to call Read() as Patrick put in his answer. I left that part out.
Steve Danner
Steve, are you assuming that there is only 1 image in the entire column??
VP
Not sure what you mean by 1 image in the entire column. I assume it's 1 image per column per row, but like I said, you'll have to call Read() like Patrick said. I edited to show.
Steve Danner
Heres the scenario. I have a table with like 20 columns one of which is the images_column. That column is the ONLY column that has images stored in as binary data. The remainig columns are usual integers (its size value) and strings (its name, location, etc) associated with that particular image. I hope that it is clear now. Thanks. I will post it up in the main question too. –
VP
Yeah it is, in that case the code above should help, but I don't know anything about Telerik's Cms Engine, so I can't be of much help beyond what's in the answer.
Steve Danner
No problem. Thanks for your help Steve
VP
A: 

Depending on how you save the image in the database, you could extract them by using the Bitmap class. It takes several different arguments to it's constructors, among others a Stream for instance.

You get the data from the reader by using its Read function. It gives you a bool to let you know if you have a valid row or not. You could use this in a if-statement or a while-statement.

Feed the data from the database column to a MemoryStream for instance and give that to the Bitmap constructor.

while (productsSqlDataReader.Read()) {
    MemoryStream stream = new MemoryStream((byte[])productsSqlDataReader["image_column"]);
    new Bitmap(stream);
}
Patrick
will this work if there are more than 1 images in the column? This looks like it takes the entire column as buffer of bytes does it? In that case, how do I take each image as a byte array?
VP
@VP: Do you have several images in the same *row* in the database, or one image per row? If you have several images in one column you need a way to determine the size of them, unfortunately image processing goes beyond my knowledge. If you do have several images in the same *cell*, may I ask why? If you have one image per cell (i.e. one per column and row) you get one in each loop in the while loop.
Patrick
Heres the scenario. I have a table with like 20 columns one of which is the images_column. That column is the ONLY column that has images stored in as binary data. The remainig columns are usual integers (its size value) and strings (its name, location, etc) associated with that particular image. I hope that it is clear now. Thanks. I will post it up in the main question too.
VP
@VP: But you state "associated with that particular image". Do you have one image *per row* or *several images in a particular cell*? If you have one image per row the above solution will work.
Patrick
I just have 1 image per row and theres ONLY 1 column in the table that has images.
VP
A: 
        string conn = "Data Source=servername\\SQL2008; Initial Catalog=MyData;UID=MyID;PWD=mypassword;";
        using (SqlConnection dbConn = new SqlConnection(conn))
        {
            dbConn.Open();
            string sql = "SELECT DATALENGTH(image_column), image_column FROM images_table ";
            using (SqlCommand cmd = new SqlCommand(sql, dbConn))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        int size = reader.GetInt32(0);
                        byte[] buff = new byte[size];
                        reader.GetBytes(1, 0, buff, 0, size);
                        MemoryStream ms = new MemoryStream(buff, 0, buff.Length);
                        ms.Write(buff, 0, buff.Length);
                        StoreImage(Image.FromStream(ms, true));
                    }
                }
            }
        }
J Edward Ellis
Thanks J. Looks pretty elegant.
VP