I want to save user image into a database in C#. How do I do that?
This question is related to
c#
sql
asp.net-mvc
ado.net
Since you are using SQL, would recommend against using adhoc ('writing statements in strings'), especially given that you are loading an image.
ADO.NET can do all of the hard work of mapping, escaping etc for you.
Either create a Stored Procedure, or use SqlParameter to do the binding.
As the other posters say, use VARBINARY(MAX) as your storage type - IMAGE is being depracated.
This is a method that uses a FileUpload control in asp.net:
byte[] buffer = new byte[fu.FileContent.Length];
Stream s = fu.FileContent;
s.Read(buffer, 0, buffer.Length);
//Then save 'buffer' to the varbinary column in your db where you want to store the image.
Try this method. It should work when field when you want to store image is of type byte
.
First it creates byte[]
for image. Then it saves it to the DB using IDataParameter
of type binary
.
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
public static void PerisitImage(string path, IDbConnection connection)
{
using (var command = connection.CreateCommand ())
{
Image img = Image.FromFile (path);
MemoryStream tmpStream = new MemoryStream();
img.Save (tmpStream, ImageFormat.Png); // change to other format
tmpStream.Seek (0, SeekOrigin.Begin);
byte[] imgBytes = new byte[MAX_IMG_SIZE];
tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);
command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
IDataParameter par = command.CreateParameter();
par.ParameterName = "payload";
par.DbType = DbType.Binary;
par.Value = imgBytes;
command.Parameters.Add(par);
command.ExecuteNonQuery ();
}
}
You'll want to convert the image to a byte[]
in C#, and then you'll have the database column as varbinary(MAX)
After that, it's just like saving any other data type.
//Arrange the Picture Of Path.***
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
string[] PicPathArray;
string ArrangePathOfPic;
PicPathArray = openFileDialog1.FileName.Split('\\');
ArrangePathOfPic = PicPathArray[0] + "\\\\" + PicPathArray[1];
for (int a = 2; a < PicPathArray.Length; a++)
{
ArrangePathOfPic = ArrangePathOfPic + "\\\\" + PicPathArray[a];
}
}
// Save the path Of Pic in database
SqlConnection con = new SqlConnection("Data Source=baqar-pc\\baqar;Initial Catalog=Prac;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("insert into PictureTable (Pic_Path) values (@Pic_Path)", con);
cmd.Parameters.Add("@Pic_Path", SqlDbType.VarChar).Value = ArrangePathOfPic;
cmd.ExecuteNonQuery();
***// Get the Picture Path in Database.***
SqlConnection con = new SqlConnection("Data Source=baqar-pc\\baqar;Initial Catalog=Prac;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Pic_Path where ID = @ID", con);
SqlDataAdapter adp = new SqlDataAdapter();
cmd.Parameters.Add("@ID",SqlDbType.VarChar).Value = "1";
adp.SelectCommand = cmd;
DataTable DT = new DataTable();
adp.Fill(DT);
DataRow DR = DT.Rows[0];
pictureBox1.Image = Image.FromFile(DR["Pic_Path"].ToString());
I think this valid question is already answered here. I have tried it as well. My issue was simply using picture edit (from DevExpress). and this is how I got around it:
Thank you again. Chagbert
you can save the path of the image in the database or save the image itself as a BLOB ( binary - array of bytes)..it depends on the case you got,if your application is a web application,then saving the path of the image is much better.but if you got a client based application that connects to a centralized database,then you must save it as binary.
My personal preference is not to save the images to a database as such. Save the image somewhere in the file system and save a reference in the database.
Source: Stackoverflow.com