//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;
}