C#操作Sql Server 2005的Image字段
2012-08-26 12:28
274 查看
前段遇到了使用Sql server 2005存储图片的问题,现已解决。将程序代码附上以备查看。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace TestPhotoSaveAndQuery
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string conncetionString = @"data source=.;database=Test;uid=sa;pwd=sa";
public static SqlConnection conn = new SqlConnection(conncetionString);
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into UserPhoto values(2,@pic)", conn);
byte[] photo;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
photo = new byte[fs.Length];
fs.Read(photo, 0, photo.Length);
fs.Close();
SqlParameter parameter = new SqlParameter("@pic", SqlDbType.Image);
parameter.Value = photo;
cmd.Parameters.Add(parameter);
conn.Open();
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result == 1)
{
MessageBox.Show("插入数据成功!");
}
else
{
MessageBox.Show("插入数据失败!");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from UserPhoto where ID=2", conn);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] photo = (byte[])ds.Tables[0].Rows[0]["PicImages"];
if (photo.Length > 0)
{
//存图片法
//string urlImage = Environment.CurrentDirectory + "\\1.jpg";
//FileStream fs = new FileStream(urlImage, FileMode.OpenOrCreate, FileAccess.Write);
//MemoryStream ms=new MemoryStream(
//BinaryWriter bw = new BinaryWriter(fs);
//bw.BaseStream.Write(photo, 0, photo.Length);
//bw.Flush();
//bw.Close();
//fs.Close();
//pictureBox1.ImageLocation = urlImage;
//直接使用数据流法
using (MemoryStream ms = new MemoryStream(photo, false))
{
pictureBox1.Image = Image.FromStream(ms);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace TestPhotoSaveAndQuery
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string conncetionString = @"data source=.;database=Test;uid=sa;pwd=sa";
public static SqlConnection conn = new SqlConnection(conncetionString);
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into UserPhoto values(2,@pic)", conn);
byte[] photo;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
photo = new byte[fs.Length];
fs.Read(photo, 0, photo.Length);
fs.Close();
SqlParameter parameter = new SqlParameter("@pic", SqlDbType.Image);
parameter.Value = photo;
cmd.Parameters.Add(parameter);
conn.Open();
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result == 1)
{
MessageBox.Show("插入数据成功!");
}
else
{
MessageBox.Show("插入数据失败!");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from UserPhoto where ID=2", conn);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] photo = (byte[])ds.Tables[0].Rows[0]["PicImages"];
if (photo.Length > 0)
{
//存图片法
//string urlImage = Environment.CurrentDirectory + "\\1.jpg";
//FileStream fs = new FileStream(urlImage, FileMode.OpenOrCreate, FileAccess.Write);
//MemoryStream ms=new MemoryStream(
//BinaryWriter bw = new BinaryWriter(fs);
//bw.BaseStream.Write(photo, 0, photo.Length);
//bw.Flush();
//bw.Close();
//fs.Close();
//pictureBox1.ImageLocation = urlImage;
//直接使用数据流法
using (MemoryStream ms = new MemoryStream(photo, false))
{
pictureBox1.Image = Image.FromStream(ms);
}
}
}
}
}
相关文章推荐
- 在sql server 2005中操作image,varbinary字段
- C# 对SQL Server中Image字段的读写(2)
- C# 对SQL Server中Image字段的读写
- C# 对SQL Server 中Image字段的读写
- sql Server 2005中datatime 类型字段模糊查询Like 操作
- SQL Server 字段和对应的说明操作(SQL Server 2005 +)
- SQL Server 对 Image字段进行操作
- 如何操作sqlserver 数据库的image字段[C#]
- Visual C# 2008+SQL Server 2005 数据库与网络开发--8.2.2 用户数据操作的并发
- SQL Server 对 Image字段进行操作
- C#操作SQLServer的Image字段
- SQL SERVER 2005(2008)插入Blob(Image)字段范例
- C#操作SQL Server中的Image类型数据
- C#操作SQL Server中的Image类型数据
- C#操作SQL Server中的Image类型数据
- Visual C# 2008+SQL Server 2005 数据库与网络开发-- 7.4 小结
- .net中使用SQL Server的Image字段类型储存图片
- sql server 2000/2005 游标的使用操作
- SQL Server 2005 XML 操作总结(五)元素(节点)操作——修改、删除、移
- 解决SQL Server的TEXT、IMAGE类型字段的长度限制