【发布时间】:2016-07-23 06:53:24
【问题描述】:
我该如何解决这个问题?我无法使用 c# 在 mysql 中查看我保存的带有图像的查询。我在 mysql 中保存图像时使用了 blob 数据类型。我是一名大学生,我们现在正在学校开展 IT 项目。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string connStr = "server = 127.0.0.1; uid = root; " + "pwd =; database = dbSample";
MySqlConnection con = new MySqlConnection(connStr);
MySqlCommand cmd;
MySqlDataAdapter da;
string query = "select * from image where name = '" + txtFirstname.Text + "' ";
cmd = new MySqlCommand(query, con);
da = new MySqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
txtFirstname.Text = dt.Rows[0][0].ToString();
byte[] ImageData = (byte[])dt.Rows[0][1]; // <----this is the error.
MemoryStream ms = new MemoryStream(ImageData);
pictureBox1.Image = Image.FromStream(ms);
da.Dispose();
}
}
}
【问题讨论】: