Code: Select all
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.InitialDirectory = @"" ;
fdlg.Filter = "Jpeg files (*.jpg)|*.jpg|MS bitmap Files (*.bmp)|*.bmp" ;
fdlg.FilterIndex = 2 ;
if(fdlg.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(fdlg.FileName, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
fs.Close();
// Method 1
dataSet = new System.Data.DataSet("Image");
string strSql = "select * from file";
MessageBox.Show(connection.ConnectionString);
mySqlConnection = new MySqlConnection(connection.ConnectionString);
mySqlConnection.Open();
mySqlDataAdapter = new MySqlDataAdapter(strSql, mySqlConnection);
mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.Fill(dataSet,"Table");
dataRow = dataSet.Tables["Table"].NewRow();
dataRow["id"] = 1;
dataRow["extension"] = fdlg.FileName;
dataRow["data"] = buffer;
dataSet.Tables["Table"].Rows.Add(dataRow);
// trying to update the table to add the image
try
{
mySqlDataAdapter.Update(dataSet,"Table");
}
catch (System.NullReferenceException msg)
{
MessageBox.Show(msg.Message, "SQL error");
}
finally
{
mySqlConnection.Close();
}
// End method 1
// Method 2
/*MySqlConnection mySqlConnection = new MySqlConnection(connection.ConnectionString);
MySqlBlob myBlob = new MySqlBlob(buffer);
MySqlCommand myCommand = new MySqlCommand("Insert into file (id, extension, data) values(1, 'something', :MyData);", mySqlConnection);
//MySqlCommand myCommand = new MySqlCommand("Insert into file set id=1, extension='something';", mySqlConnection);
myCommand.Parameters.Add("MyData", myBlob);
mySqlConnection.Open();
try
{
myCommand.ExecuteNonQuery();
}
catch (System.NullReferenceException msg)
{
MessageBox.Show(msg.Message, "SQL error");
}
finally
{
mySqlConnection.Close();
}*/
// End method 2
}
Thanks
John