c# winform数据库中存储图片是二进制的,现在却不能在datagridview中显示出来,求高手支招。
求具体代码。 展开
那个老师教你用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# 图片 自己区分!我想你能看懂:
很简单的查询语句显示在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样式呢?
应该不是设置样式的问题.你可以打个断点调试一下,看看是那里出错了.大断点很容易找出错误并解决的.. 另外 . 楼上的追问很犀利..我喜欢...哈哈哈