Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to save image into database in windows applications
Posted

Try this method. It should work when field when you want to store image is of type bytea. First it creates byte[] for image. Then it saves it DB using IDataParameter of type binary.

C#
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 ();
        }
    }
 
Share this answer
 
Please refer following threads:

A sample program to demonstrate how to save or store images in SQL server:
Store or Save images in SQL Server[^]

This sample code explains you how you can store or save images in SQL Server database using C#. It uses ADO.Net System.Data.SqlClient namespace
Store or save images in SQL Server database using C#[^]

Inserting image into database using C# windows application.[^]
 
Share this answer
 
Comments
Balaram224 16-Feb-16 5:11am    
what is this inidanblubook.com site permalink ?????????
 
Share this answer
 
//use this method to open an image

public Byte[] StoreImageTODB()
{
FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read);
// path is the actual path to the image
Byte[] byteimagedata = new Byte[fs.Length];
fs.Read(byteimagedata,0,byteimagedata.Length);
fs.Close();
return byteimagedata;
}

// Then you can call it where you are going to store the image to the database.

public void InsertImage()
{
bool status;
Byte[] imagedata = StoreImageTODB();// call above method and get image to Byte array
string ID = "01"; // just as an example
status=dba.Insert_Image(ID,imagedata); // dba is the object of the DBACCESS class

if(status)
messageBox.Show("Success");

}

// In DBACCESS class you can catch those parameters

public bool InsertNewEmp(string ID,Byte[] image) // insret new data
{
bool status = false;
try
{


if (conn.State.ToString() == "Closed")
{
conn.Open();
}

SqlCommand newCmd = conn.CreateCommand();

newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "insert into ImageDetails(ID,ID_Image) values('" +ID + "',@Image)";
SqlParameter prm = new SqlParameter("@Image", SqlDbType.VarBinary, image.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, image);
newCmd.Parameters.Add(prm);

newCmd.ExecuteNonQuery();

status = true;


}
catch (Exception ex)
{
MessageBox.Show("Your Database connection has an error !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return status;
}
C#

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900