- 论坛徽章:
- 0
|
关于将图片存入mysql的问题
我用procedure来做的. fyi:
// Get data
FileInfo file = new FileInfo(fileName);
int fileLength = Convert.ToInt32(file.Length);
byte[] imageData = new byte[fileLength];
String imageTitle = file.Name;
String imageType = file.Extension;
// Read data from file to a byte array.
FileStream fs = file.OpenRead();
fs.Read(imageData, 0, fileLength);
// Build MySqlCommand
String strCom = "usp_imageinsert";
MySqlCommand com = new MySqlCommand(strCom, this.ConnDB());
com.CommandType = CommandType.StoredProcedure;
MySqlParameter para = com.Parameters.Add( "p_imageid", MySqlDbType.Int32 );
para.Direction = ParameterDirection.Output;
com.Parameters.Add( "p_imagetitle", MySqlDbType.VarChar, 50 );
com.Parameters.Add( "p_imagetype", MySqlDbType.VarChar, 50 );
com.Parameters.Add( "p_imagedata", MySqlDbType.LongBlob );
com.Parameters["p_imagetitle"].Value = imageTitle;
com.Parameters["p_imagetype"].Value = imageType;
com.Parameters["p_imagedata"].Value = imageData;
try
{
// Insert into database
com.ExecuteNonQuery();
con.Close();
MessageBox.Show( "OK" );
}
catch(Exception ex)
{
MessageBox.Show( ex.ToString() );
} |
|