|
Private void button2_Click_1 (object sender, System. EventArgs e)
{
String pathName;
If (this. openFileDialog1.ShowDialog () = System. Windows. Forms. DialogResult. OK)
{
PathName = this. openFileDialog1.FileName;
System. Drawing. Image img = System. Drawing. Image. FromFile (pathName );
This. pictureBox1.Image = img;
// Read the image into a byte array
System. IO. FileStream fs = new System. IO. FileStream (pathName, System. IO. FileMode. Open, System. IO. FileAccess. Read );
Byte [] buffByte = new byte [fs. Length];
Fs. Read (buffByte, 0, (int) fs. Length );
Fs. Close ();
Fs = null;
// Create Command
String comm = @ "Insert into table1 (img, name) values (@ img, @ name )";
This. sqlCommand1 = new System. Data. SqlClient. SqlCommand ();
This. sqlCommand1.CommandType = System. Data. CommandType. Text;
This. sqlCommand1.CommandText = comm;
This. sqlCommand1.Connection = this. sqlConnection1;
// Create a Parameter
This. sqlCommand1.Parameters. Add ("@ img", System. Data. SqlDbType. Image );
This. sqlCommand1.Parameters [0]. Value = buffByte;
This. sqlCommand1.Parameters. Add ("@ name", System. Data. SqlDbType. VarChar );
This. sqlCommand1.Parameters [1]. Value = pathName. Substring (pathName. LastIndexOf ("\") + 1 );
Try
{
This. sqlConnection1.Open ();
This. sqlCommand1.ExecuteNonQuery ();
This. sqlConnection1.Close ();
}
Catch (System. Exception ee)
{
MessageBox. Show (ee. Message );
}
BuffByte = null;
This. FillListBox ();
} |