无法从数据库MySqL C#获取图像

时间:2017-09-28 12:01:09

标签: c# mysql

我正在尝试从Mysql数据库获取信息以显示完整信息和护照照片。但似乎有一个挑战。它在Read()之前告诉我无效错误,但仍然没有显示护照。但它在编译时不会出错。

注意:行参数是Mysql数据库中的护照。

我的代码现在看起来像这样

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 MetroFramework;
using MySql.Data.MySqlClient;
using System.IO;

namespace VisitorManager2
{

    public partial class VisitSignout : MetroFramework.Forms.MetroForm
    {

        public VisitSignout()
        {

            InitializeComponent();
        }

        private void VisitSignout_Load(object sender, EventArgs e)
        {

        }

        private void metroTextBox9_Click(object sender, EventArgs e)
        {

        }

        private void metroLabel9_Click(object sender, EventArgs e)
        {

        }

 /*       private Image getPasport()
        {
            string constring = "server=localhost;user id=root;database=vms2";
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                string query = "select passport from vms2.vist_db";
                MySqlDataAdapter da = new MySqlDataAdapter(query, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {

                }
            }

            return img ;
        }*/

        private void metroButton3_Click(object sender, EventArgs e)
        {
            string constring = "server=localhost;user id=root;database=vms2";
            string query = "select * from visit_db where visitor_name='" + metroTextBox10.Text.Trim() + "'";

            using (MySqlConnection con = new MySqlConnection(constring))
            {
                try
                {
                    DataTable dt = new DataTable();
                    con.Open();
                    MySqlDataReader rdr = null;
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                    rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        metroTextBox1.Text = (rdr["visit_date"].ToString());
                        metroTextBox2.Text = (rdr["visit_time"].ToString());
                        metroTextBox3.Text = (rdr["visitor_name"].ToString());
                        metroTextBox4.Text = (rdr["meeting_employee"].ToString());
                        metroTextBox5.Text = (rdr["organization"].ToString());
                        metroTextBox6.Text = (rdr["phone"].ToString());
                        metroTextBox7.Text = (rdr["visit_type"].ToString());
                        metroTextBox8.Text = (rdr["department"].ToString());

                        var ds = new DataSet();
                        da.Fill(ds, "passport");
                        int count = ds.Tables["passport"].Rows.Count;
                        if (count > 0)
                        {
                            var data = (Byte[])ds.Tables["passport"].Rows[count - 1]["passport"];
                            var stream = new MemoryStream(data);
                            pictureBox1.Image = Image.FromStream(stream);
                        }
                    }

                    con.Close();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}

我可能会错过什么?

1 个答案:

答案 0 :(得分:5)

private const string connPath = "datasource=localhost;username=root;password='';database=lms;";
    public MySqlConnection MyConn = new MySqlConnection(connPath);
    public DataTable selectQuery(string query)
    {
        DataTable dTable = new DataTable();
        try
        {                
            MySqlCommand MyCommand = new MySqlCommand(query, MyConn);
            MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
            MyAdapter.SelectCommand = MyCommand;
            MyAdapter.Fill(dTable);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        return dTable;
    }

DataTable mem = selectQuery("SELECT image FROM `mem_det` WHERE Member_id=1");
        int count = mem.Rows.Count;
        if (count > 0)
        {
            var data = (Byte[])(mem.Rows[count - 1]["image"]);
            var stream = new MemoryStream(data);
            memberImage.Image = Image.FromStream(stream);
        }