How to insert image in database in C#
using System;
namespace CarShowRoom
{
public partial class frmSale : DevExpress.XtraEditors.XtraForm
{
SqlConnection con = CString.con;
MemoryStream ms;
byte[] photo_aray;
public frmSale()
{
InitializeComponent();
}
public void btnInsert()
{
string query;
if (pictureEdit1.Image != null)
{
query = "INSERT INTO Sale(ID,Image) VALUES (@txtID,@photo)";
}
else
{
"INSERT INTO Sale(ID) VALUES (@txtID)";
}
try
{
using (SqlCommand command = new SqlCommand(query, con))
{
command.Parameters.AddWithValue("@txtID", txtID.EditValue);
if (pictureEdit2.Image != null)
{
ms = new MemoryStream();
pictureEdit2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
command.Parameters.AddWithValue("@photo", photo_aray);
}
con.Open();
int result = command.ExecuteNonQuery();
// Check Error
if (result < 0)
XtraMessageBox.Show("Error");
con.Close();
}
}
catch (Exception ex)
{
XtraMessageBox.Show("ITEM" + ex.Message);
}
}
}
}
No comments:
Post a Comment