Uploading Images to a Database

Working with the Uploaded Image

Once the user posts the data, we have to be able to parse the binary data and send it to the database. Along with the main body of the code, we use a helper function called SaveToDB() to achieve this.

private int SaveToDB(string imgName, byte[] imgbin, string imgcontenttype)
{
    //use the web.config to store the connection string
    SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
    SqlCommand command = new SqlCommand( "INSERT INTO Image (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )", connection );
    SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
    param0.Value = imgName;
    command.Parameters.Add( param0 );
    SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
    param1.Value = imgbin;
    command.Parameters.Add( param1 );
    SqlParameter param2 = new SqlParameter( "@img_contenttype", SqlDbType.VarChar,50 );
    param2.Value = imgcontenttype;
    command.Parameters.Add( param2 );
    connection.Open();
    int numRowsAffected = command.ExecuteNonQuery();
    connection.Close();
    return numRowsAffected;
}


In this function we are passing in 3 different parameters

imgName - the friendly name we want to give out image data
imgBin - the binary or Byte array of our data
imgContentType - the content type of our image. For example: image/gif or image/jpeg

There are 3 parameters as SQLParameters and defines the type. Our first SQLParameter is @img_name and is defined as a VarChar with a length of 50. The 2nd parameter, @img_data, is the binary or Byte() of data and is defined with a data type of Image. The last parameter is @img_contenttype, is defined as a VarChar with a length of 50 characters. The remainder of the function opens a connection to the database and executes the command by calling command.ExecuteNonQuery().

Calling our functions

Ok, now that we have our worker functions written, let's go ahead and get our image data.

Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
string imgContentType = UploadFile.PostedFile.ContentType;
string imgName = txtImgName.Value;
byte[] imgBinaryData = new byte[imgLen];

We need to access three important pieces of data for our example. We need the image:

Name (imgName_value)
Content-Type (imgContentType)
Image Data (imgBindaryData)

First we access to the image stream, which we are able to get by using the property UploadFile.PostedFile.InputStream. (Remember, UploadFile was the name of our upload control on the webform). We also need to know how long the Byte array we are going to create needs to be. We can get this number by calling UploadFile.PostedFile.ContentLength, and storing it's value in imgLen.

Once we have the length of the image, we create a byte array by byte[] imgBinaryData = new byte[imgLen]; We access the content type of the image by accessing the ContentType property of UploadFile.PostedFile. Lastly we need the friendly name we are going to use for the image.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow