0
An unhandled exception of type 'System.InvalidCastException' occurred in WindowsFormsApplication1.exe and Additional information: Unable to cast object of type 'System.String' to type 'System.Byte[]'.誤差的圖像DATATYPES
嗨,只想問如何解決這一問題?我無法使用c#在mysql中查看保存的查詢。我使用blob數據類型在mysql中保存圖像。而我是一名大學生,我們現在正在學校裏開展IT項目:)期待答案:) Godbless。 TIA。 :d
這是我的代碼:
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();
}
}
}
感謝先生:)對不起,浪費你的時間,我只是在編程初學者。我應該在那裏放什麼?我還沒有想法添加圖像並在視窗窗體中檢索它。 TIA。 :) –
[請嘗試此鏈接](http://www.c-sharpcorner.com/UploadFile/8ea152/fetch-image-from-database-in-windows-form-application/):) –