将图像插入 Mysql 时出错
Error in inserting the image to Mysql
我正在使用 C# 将图像插入 Mysql 数据库。字节数组的长度是51115。但是,当我检查数据库时,字段IMage(Long Blob)的长度只有13个字节。这是什么错误?请帮助我!
private void saveBtn_Click(object sender, EventArgs e)
{
try
{
Image img = imgBox.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
MessageBox.Show(arr.Length.ToString()); // 51115
string connectionString = "server=localhost;user id=root;password=xrayadmin;database=xraydatabase";
string query = "INSERT INTO imagedatabase(IDPatient,Image,DiagnosisDoctor,DiagnosisDate) VALUES('" + Convert.ToInt32(labPatID.Text) + "','" +
arr + "','" + labDocUser.Text + "','" + DateTime.Now.ToString("dd / MM / yyyy") + "')";
MySqlConnection MysqlConnection = new MySqlConnection(connectionString);
MySqlCommand MysqlCmd = new MySqlCommand(query, MysqlConnection);
MySqlDataReader DataReader;
MysqlConnection.Open();
DataReader = MysqlCmd.ExecuteReader();
MessageBox.Show("Save Data");
while (DataReader.Read())
{
}
MysqlConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
我推荐你使用参数。像这样
string query = @"INSERT INTO imagedatabase(IDPatient,Image,DiagnosisDoctor,DiagnosisDate) VALUES(@IDPatient,@Image,@DiagnosisDoctor,@DiagnosisDate)"
MysqlCmd.Parameters.AddWithValue("@Image", arr);
使用字符串连接时,数组将无法正确处理。
您需要使用查询参数并将字节数组传递给参数。 .net 数据库函数将为您正确处理二进制数据。有关示例,请参见 Parameterized Query for MySQL with C#。
搜索堆栈溢出或您最喜欢的搜索引擎将针对您正在使用的任何数据类型在正确的参数类型上产生大量结果。
顺便说一句,通过使用字符串连接来编写您的查询,您正在创造 巨大的 风险 SQL 注射(如果这是医疗数据作为您代码表明,在美国和许多其他公司,泄露此类数据的罚款非常昂贵)。这是一个非常非常糟糕的习惯,最好尽快改掉。
我正在使用 C# 将图像插入 Mysql 数据库。字节数组的长度是51115。但是,当我检查数据库时,字段IMage(Long Blob)的长度只有13个字节。这是什么错误?请帮助我!
private void saveBtn_Click(object sender, EventArgs e)
{
try
{
Image img = imgBox.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
MessageBox.Show(arr.Length.ToString()); // 51115
string connectionString = "server=localhost;user id=root;password=xrayadmin;database=xraydatabase";
string query = "INSERT INTO imagedatabase(IDPatient,Image,DiagnosisDoctor,DiagnosisDate) VALUES('" + Convert.ToInt32(labPatID.Text) + "','" +
arr + "','" + labDocUser.Text + "','" + DateTime.Now.ToString("dd / MM / yyyy") + "')";
MySqlConnection MysqlConnection = new MySqlConnection(connectionString);
MySqlCommand MysqlCmd = new MySqlCommand(query, MysqlConnection);
MySqlDataReader DataReader;
MysqlConnection.Open();
DataReader = MysqlCmd.ExecuteReader();
MessageBox.Show("Save Data");
while (DataReader.Read())
{
}
MysqlConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
我推荐你使用参数。像这样
string query = @"INSERT INTO imagedatabase(IDPatient,Image,DiagnosisDoctor,DiagnosisDate) VALUES(@IDPatient,@Image,@DiagnosisDoctor,@DiagnosisDate)"
MysqlCmd.Parameters.AddWithValue("@Image", arr);
使用字符串连接时,数组将无法正确处理。
您需要使用查询参数并将字节数组传递给参数。 .net 数据库函数将为您正确处理二进制数据。有关示例,请参见 Parameterized Query for MySQL with C#。
搜索堆栈溢出或您最喜欢的搜索引擎将针对您正在使用的任何数据类型在正确的参数类型上产生大量结果。
顺便说一句,通过使用字符串连接来编写您的查询,您正在创造 巨大的 风险 SQL 注射(如果这是医疗数据作为您代码表明,在美国和许多其他公司,泄露此类数据的罚款非常昂贵)。这是一个非常非常糟糕的习惯,最好尽快改掉。