c# winform数据库中存储图片是二进制的,现在却不能在datagridview中显示出来,求高手支招。

2024年11月18日 16:20
有2个网友回答
网友(1):

那个老师教你用datagridview显示图片!用pictureBox不是更好吗?我给你一段源程序!你好好看看!数据库我也给你!只是图片!

源代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.IO;

using System.Data.SqlTypes;

namespace BLOB

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            UpdateTime.Text = System.DateTime.Now.ToString();

        }

        string PhotoName = "";

        byte[] Pic;

        private void BSelect_Click(object sender, EventArgs e)

        {

            openFileDialog1.Filter = "选择图片(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files(*.*)|*.*";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName,true);//牛逼啊、、、、、、、、、、、、

                PhotoName = openFileDialog1.FileName;

                Pname.Text = PhotoName.Substring(PhotoName.LastIndexOf(@"\") + 1);//;;;;;;;;;;;;;;;;;;;1·11!!

            }

            else

            {

                MessageBox.Show("您还未选取图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;

            }

           

        }

        private void BAdd_Click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Contact;Integrated Security=True");

            try

            {

                conn.Open();

                SqlCommand cmd = new SqlCommand("insert into BLOB values(@name,@time,@photo)", conn);

                cmd.Parameters.AddWithValue("@name", Pname.Text);

                cmd.Parameters.AddWithValue("@time", UpdateTime.Text);

                Pic = File.ReadAllBytes(PhotoName);

                cmd.Parameters.AddWithValue("@photo", Pic);

                cmd.ExecuteNonQuery();

                conn.Close();

                MessageBox.Show("插入数据成功!", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Contact;Integrated Security=True");

            if (conn.State == ConnectionState.Closed)

            {

                conn.Open();

            }

            SqlCommand cmd = new SqlCommand("select name from BLOB",conn);

            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())

            {

                list.Items.Add(dr.GetString(0));

            }

            

        }

        private void list_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (list.SelectedIndex == -1)

            {

                return;

            }

            else

            {

                SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Contact;Integrated Security=True");

                if (conn.State == ConnectionState.Closed)

                {

                    conn.Open();

                }

                string SelectItem = list.SelectedItem.ToString();

                SqlCommand cmd = new SqlCommand("Select * from BLOB where name=@SelectItems",conn);

                cmd.Parameters.AddWithValue("@SelectItems", SelectItem);

                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())

                {

                    Pname.Text = dr.GetString(0);

                    UpdateTime.Text = dr.GetDateTime(1).ToString();

                    SqlBytes PhotoBytes = dr.GetSqlBytes(2);

                    pictureBox1.Image = Image.FromStream(PhotoBytes.Stream);

                }

                dr.Close();

                conn.Close();  //最好加上这两个CLOSE()方法!

                

            }

            

        }

        

    }

}

数据库图片 和 C# 图片 自己区分!我想你能看懂:

网友(2):

你代码写的有问题嘛。。不贴代码怎么给你解决。。