Sai Yang

Sai Yang

  • 1.9k
  • 128
  • 12.4k

C# how to insert image ????

Apr 23 2018 1:12 AM
hi 
 
I need to insert a image to my table but I confuse with my connection string because I declare in a function: 
 
public static string conn = "server=.\\SQLEXPRESS; database=db_Soking; user=admin; password=123; Integrated Security=True"; 
 
public static DataTable getdata(string sql, string tbname)
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(conn));
da.Fill(ds, tbname);
dt = ds.Tables[tbname];
return dt;
}
 
Then I call in insert statement like:
 
string upcon = "insert into tb_name ........";
DataTable dtcon = ClassConnection.getdata(upcon, "tbname");
 
this mean already done.
 
but this code doesn't support for an image parameter. I need to insert with an image parameter like the below style:
 
byte[] imgbt = null;
FileStream fs = new FileStream(pc, FileMode.Open, FileAccess.Read);
BinaryReader bdr = new BinaryReader(fs);
imgbt = bdr.ReadBytes((int)fs.Length);
string einsert = "insert into tb_name.......,@IMG)";
MySqlCommand ecmdinsert = new MySqlCommand(einsert, Classfontstore.fo);
ecmdinsert.Parameters.Add(new MySqlParameter("@img", imgbt));
MySqlDataReader edrinsert = ecmdinsert.ExecuteReader(); 
 
MessageBox.Show("Insert successful", "Information Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
in my connectionstring I don't have sqlcommand caused I cannot add parameter.
 
Question
1. where/how can I declare my sqlcommand and parameter? 
 
 2. any where it's easily to declare it?
 
If any one has solution please help me in advance.
 
Thank you so much. 

Answers (1)