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

c#winform数据库中存储图片是二进制的,现在却不能在datagridview中显示出来,求高手支招。前两个可以显示,第三个和第四个不能显示。什么原因???如图报错。... c# winform数据库中存储图片是二进制的,现在却不能在datagridview中显示出来,求高手支招。前两个可以显示 ,第三个和第四个 不能显示。什么原因???如图 报错。
求具体代码。
展开
 我来答
百度网友387227639
2011-11-08 · TA获得超过143个赞
知道答主
回答量:85
采纳率:0%
帮助的人:75.7万
展开全部

那个老师教你用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# 图片 自己区分!我想你能看懂:

_请开始你的表演
2011-11-04 · 超过71用户采纳过TA的回答
知道小有建树答主
回答量:298
采纳率:0%
帮助的人:172万
展开全部
你代码写的有问题嘛。。不贴代码怎么给你解决。。
追问
很简单的查询语句显示在datagridview中。Maticsoft.Model.T_NewPatient newp = new Maticsoft.Model.T_NewPatient();     newp.PatientName = textBox1.Text;
Maticsoft.BLL.T_NewPatient newpa = new Maticsoft.BLL.T_NewPatient(); DataSet ds = newpa.GetList(" PatientName ='"+textBox1.Text+"'"); dataGridView1.DataSource=ds.Tables[0].DefaultView;是不是显示图片的那个cell要设为imagecolumn样式呢?
追答
应该不是设置样式的问题.你可以打个断点调试一下,看看是那里出错了.大断点很容易找出错误并解决的..     另外 .  楼上的追问很犀利..我喜欢...哈哈哈
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式