====部分代码==== byte[] myphoto = null; MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); myphoto = new Byte[ms.Length]; ms.Position = 0; ms.Read(myphoto, 0, (Int32)ms.Length);
string sqlStr = "insert into photo (name,photo) values ('XXX',myphoto)"; MySQLCommand cmd = new MySqlCommand(sqlStr, conn); conn.Open(); cmd.ExecuteNonQuery();//运行到此处出错 cmd.Dispose(); conn.Close();
出错信息为:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘System.Byte[])' at line 1
====修改后代码能运行==== byte[] myphoto = null; MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); myphoto = new Byte[ms.Length]; //ms.Position = 0; //ms.Read(myphoto, 0, (Int32)ms.Length);
string sqlStr = "insert into photo (name,photo) values ('XXX',myphoto)"; MySQLCommand cmd = new MySqlCommand(sqlStr, conn); conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close();
也就是将数据类型为 byte[] 的 myphoto 在不填充图片数据时能向 MySQL 中写入相同长度的空字节数组时程序运行正常,只是放入MySQL数据库LongBlob字段的数据全部为 0000 0000.......。 我没试用其他数据填充在字节数组中是否能写入数据库中。