Skip to content

Instantly share code, notes, and snippets.

@Benrnz
Created February 10, 2009 01:09
Show Gist options
  • Save Benrnz/61137 to your computer and use it in GitHub Desktop.
Save Benrnz/61137 to your computer and use it in GitHub Desktop.
//Get a file and save as blob to a db
//Upload from file .png
string lFileName = @"C:\Inetpub\wwwroot\welcome.png";
Image lOriginalImage = Image.FromFile(lFileName);
//ImageFormatConverter lConv = new ImageFormatConverter(); //Use to convert to different formats
MemoryStream lMs = new MemoryStream();
lOriginalImage.Save(lMs, ImageFormat.Png);
Byte[] lBuffer = lMs.GetBuffer();
lMs.Close();
SqlConnection lConn = new SqlConnection(fConn);
string lSQL = "INSERT INTO Picture (Filename, Image)" + "VALUES (@Filename, @Image)";
SqlCommand lCmd = new SqlCommand(lSQL, lConn);
lCmd.Parameters.Add(new SqlParameter("@Filename", SqlDbType.NVarChar, 50)).Value = lFileName;
lCmd.Parameters.Add(new SqlParameter("@Image", SqlDbType.Image)).Value = lBuffer;
lConn.Open();
lCmd.ExecuteNonQuery();
lConn.Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment