mysql 插入图片报错_c# 向mysql存入图片报错,错误是语法错误。代码片段图片如下...

展开全部

这问题你之前问过,我估计是你用的mysql驱动的问题,今天有时32313133353236313431303231363533e58685e5aeb931333337623430间给你测试了一下,首先用的驱动是官方的mysql connector net 6.9.4,数据表里两列,一个id一个picture,后一个是longblobstatic string connstring = "Server=localhost;Uid=root;Password=12345;Database=demo";

private void button6_Click(object sender, EventArgs e)//插入数据并读取

{

FileStream fs = new FileStream(@"d:\Snap2.png", FileMode.Open, FileAccess.Read);

byte[] byImage = new byte[fs.Length];

fs.Read(byImage, 0, byImage.Length);

fs.Close();

string query = "update sortlist set picture=@fs where id=1";

if (InsertImg(connstring, query, byImage) > 0) MessageBox.Show("保存完成");

Image img = LoadImg(1);

if (img != null) pictureBox3.Image = img;

}

public static int InsertImg(string connectionString,string strSQL, byte[] fs)

{

using (MySqlConnection connection = new MySqlConnection(connectionString))

{

MySqlCommand cmd = new MySqlCommand(strSQL, connection);

MySqlParameter myParameter = new MySqlParameter("@fs",MySqlDbType.LongBlob);

myParameter.Value = fs;

cmd.Parameters.Add(myParameter);

try

{

connection.Open();

int rows = cmd.ExecuteNonQuery();

return rows;

}

catch (MySqlException e)

{

throw e;

}

finally

{

cmd.Dispose();

connection.Close();

}

}

}

public static Image LoadImg(int id)

{

using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection())

{

conn.ConnectionString = connstring;

conn.Open();

MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

cmd.CommandType = CommandType.Text;

cmd.CommandText = "select id,picture from sortlist where id =" + id ;

cmd.Connection = conn;

System.Data.Common.DbDataReader reader = cmd.ExecuteReader();

byte[] buffer = null;

if (reader.HasRows)

{

reader.Read();

long len = reader.GetBytes(1, 0, null, 0, 0);//1是picture

buffer = new byte[len];

len = reader.GetBytes(1, 0, buffer, 0, (int)len);

System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);

System.Drawing.Image iamge = System.Drawing.Image.FromStream(ms);

return iamge;

}

}

return null;

}

这是最终的图

9a28700556ea5c4659a542cd3d68cb30.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值