.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Writing BLOB Data to the Database

Posted By:Shashi Ray       Posted Date: November 27, 2009    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  

Writing BLOB Data to the Database

The following code shows how to use ADO.NET to write binary data obtained from a file to an image field in SQL Server.

public void StorePicture( string filename )
  // Read the file into a byte array
  using(FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
    byte[] imageData = new Byte[fs.Length];
    fs.Read( imageData, 0, (int)fs.Length );
  using( SqlConnection conn = new SqlConnection(connectionString) )
    SqlCommand cmd = new SqlCommand("StorePicture", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@filename", filename );
    cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
    cmd.Parameters.Add("@blobdata", SqlDbType.Image);
    cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
    // Store the byte array within the image field
    cmd.Parameters["@blobdata"].Value = imageData;

Shashi Ray


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend