I cannot seem to update the image in my database no matter what I try to do. This code works out perfectly fine, but it must be uploading the image wrong or is missing data. The size is correct along with the rawdata (bytes). What is wrong here?
public static string SqlString(Byte[] bytes)
{
return "'" + ByteArrayToString(bytes).Replace("\\", "\\\\").Replace("'", "\\'") + "'";
}
private static string ByteArrayToString(byte[] b)
{
int len = b.Length;
char[] chars = new char[len];
for (int j = 0; j < len; j++)
{
chars[j] = (char)b[j];
}
return new String(chars); // this line is the kicker
}
public static void UploadImage(GINtechSysUser gintechuser, string photopath)
{
UInt32 FileSize;
byte[] rawData;
FileStream fs;
fs = new FileStream(photopath, FileMode.Open, FileAccess.Read);
FileSize = Convert.ToUInt32(fs.Length);
rawData = new byte[FileSize];
fs.Read(rawData, 0, (int)FileSize);
fs.Close();
MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand();
MySqlConnection mysqlconnection = new MySqlConnection();
mysqlconnection.ConnectionString = "Server=gsystemsmysql.db.6490710.hostedresource.com;uid=gsystemsmysql;pwd=Programs00;Database=gsystemsmysql;";
mysqlconnection.Open();
command.CommandText = "UPDATE users SET profileimage='?photo'" + ", profileimage_name='" + gintechuser.ProfileImageName + "'" + ", profileimage_size='" + FileSize.ToString() + "'" + ", profileimage_type='image/pjpeg' WHERE userid='" + gintechuser.UserID + "'";
command.Connection = mysqlconnection;
MySqlParameter para = new MySqlParameter("?photo", MySqlDbType.MediumBlob);
para.Value = rawData;
command.Parameters.Add(para);
MySql.Data.MySqlClient.MySqlDataReader myData;
myData = command.ExecuteReader();
myData.Close();
mysqlconnection.Close();
}
public static string SqlString(Byte[] bytes)
{
return "'" + ByteArrayToString(bytes).Replace("\\", "\\\\").Replace("'", "\\'") + "'";
}
private static string ByteArrayToString(byte[] b)
{
int len = b.Length;
char[] chars = new char[len];
for (int j = 0; j < len; j++)
{
chars[j] = (char)b[j];
}
return new String(chars); // this line is the kicker
}
public static void UploadImage(GINtechSysUser gintechuser, string photopath)
{
UInt32 FileSize;
byte[] rawData;
FileStream fs;
fs = new FileStream(photopath, FileMode.Open, FileAccess.Read);
FileSize = Convert.ToUInt32(fs.Length);
rawData = new byte[FileSize];
fs.Read(rawData, 0, (int)FileSize);
fs.Close();
MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand();
MySqlConnection mysqlconnection = new MySqlConnection();
mysqlconnection.ConnectionString = "Server=gsystemsmysql.db.6490710.hostedresource.com;uid=gsystemsmysql;pwd=Programs00;Database=gsystemsmysql;";
mysqlconnection.Open();
command.CommandText = "UPDATE users SET profileimage='?photo'" + ", profileimage_name='" + gintechuser.ProfileImageName + "'" + ", profileimage_size='" + FileSize.ToString() + "'" + ", profileimage_type='image/pjpeg' WHERE userid='" + gintechuser.UserID + "'";
command.Connection = mysqlconnection;
MySqlParameter para = new MySqlParameter("?photo", MySqlDbType.MediumBlob);
para.Value = rawData;
command.Parameters.Add(para);
MySql.Data.MySqlClient.MySqlDataReader myData;
myData = command.ExecuteReader();
myData.Close();
mysqlconnection.Close();
}