Wednesday, December 7, 2011

Writing BLOG Data to the Database



Writing BLOG Data to the Database
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;
     conn.Open();
     cmd.ExecuteNonQuery();
   conn.Close();
   }
 }