有人问到图片如何存储到数据库中,以前也做过,但年代已有点久远,选择使用的软件都升级了,有些东西已废弃。所以花了点时间用VS2008+SQL Server 2005做了一个测试程序,希望能对人有所帮助。与人玫瑰,手有余香。涉及到存储过程,ADO.NET操纵数据库,SQL Server 2005中的image(或varbinary(max))字段使用。
在SQL Server 2005中建立数据库、表、存储过程,SQL如下:
create database test
go
use test
create table tblImage(
[id] int identity(1,1) primary key,
picture varbinary(max))--或picture image
go
--插入图片到数据库表中的varbinary(max)类型(或image)的存储过程,需要文件名及表名参数(有默认值,可不提供此参数)
create proc spInsertImage(@fileName nvarchar(100), @tableName nvarchar(20)='tblImage') as
begin
set nocount on
declare @sql nvarchar(max)
set @sql='insert into ' + @tableName +
' select * from OPENROWSET (bulk ''' + @fileName +''', SINGLE_BLOB) as myTable'
exec(@sql)
end
在VS2008(C#)中新建项目,名为test,WinForm命名为frmImage,先在窗体上添加1个PictureBox,3个Button,1个OpenFileDialog;cs文件中代码如下:
using System;
using System.IO;//自己引入
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;//自己引入
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class frmImage : Form
{
public frmImage()
{
InitializeComponent();
}
DataSet ds = new DataSet();
SqlDataAdapter da;
//连接串,express版,集成认证方式
string cnnString = @"integrated security=true;database=test;server=.\sqlexpress";
string fileName="";
//项目名为test,WinForm命名为frmImage
//先在窗体上添加1个PictureBox,3个Button,1个OpenFileDialog
private void frmImage_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
//显示表中的第index+1行的第2列(binary(max)类型)到pictureBox1
private void showImage(int index)
{
byte[] bytes = (byte[])ds.Tables[0].Rows[index][1];
MemoryStream memStream = new MemoryStream(bytes);
try
{
Bitmap myImage = new Bitmap(memStream);
pictureBox1.Image = myImage;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
pictureBox1.Image = null;
}
}
//打开文件,获得一张图片的文件名
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "*.jpg|*.*";
openFileDialog1.FilterIndex = 0;
openFileDialog1.FileName="";
openFileDialog1.Title = "打开文件";
openFileDialog1.ShowDialog();
fileName=openFileDialog1.FileName;
}
//插入图片到数据表tblImage中
private void button2_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(cnnString);
cnn.Open();
SqlCommand cmd = new SqlCommand("spInsertImage", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fileName", fileName);
//@tableName使用默认参数值,下面这句可不用
//cmd.Parameters.AddWithValue("@tableName", "tblImage");
try
{
//调用存储过程spInsertImage
cmd.ExecuteNonQuery();
MessageBox.Show("成功插入一张图片到数据库中!");
}
catch
{
MessageBox.Show("出错了,请确认是否正确选择了图片文件!");
}
finally
{
cnn.Close();
}
}
//随机显示一张图片
private void button3_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(cnnString);
cnn.Open();
da = new SqlDataAdapter("select * from tblImage", cnn);
da.Fill(ds, "testImage");
Random ran = new Random();
int index = ran.Next(ds.Tables[0].Rows.Count);
showImage(index);
}
}
}
分享到:
相关推荐
WinForm中将一张图片插入到数据库。在数据库中创建一张表,有一个image的字段,然后通过WinForm界面选择中一张图片,并插入到该字段中。
C#winform上传图片到数据库源码,把图片转化成image格式保存到数据库中
主要示范C# winform 上传的图片以二进制流形式储存到数据库 以及 从数据库中展示图片的Demo
winform对数据库进行操作插入图片等信息c#更新
c#图片存储到数据库中,c# winform里面做的~~~~
C# 向Winform窗体拖放图片并显示。将电脑上的任意目录下的图片拖放到窗体中,窗体即可显示该图片。可应用到头像的设置中等。
winform把图片转换成IO流存到数据库 关键代码示例
在winform中实现对任意文件夹下的图片显示,并将图片路径显示在textbox中
1、listview大图显示图片; 2、图片自动加边框; 3、contextmenustrip添加右键按钮,修改图片
WInform 程序 将Excel导入SQLSERVER数据库
将图片存储到数据库的源代码。 详细解释在我的博客: http://blog.csdn.net/frombegintoend/article/details/7704131
WinForm读取Excel+并导入到数据库相应的表里 winForm c#DOC文档
NULL 博文链接:https://heisetoufa.iteye.com/blog/262798
C# Winform访问Oracle数据库
由于asp.net中的Image控件是在System.Web.UI.WebControls命名空间中,所以不能像在winform中那样通过byte[]直接显示图片。 既然这样,就只能曲线救国了。这里的例子是这样的思路:teacherdetial.aspx是最终用来...
winform的自定义控件,可以在设计时选择资源窗口
学生成绩查询程序,winForm+access实现数据库相关读写操作。 C#语言开发
内容概要:简单的C# winform调用存储过程实例,创建存储过程入参,通过SqlConnection对象和SqlCommand对象调用存储过程,获取存储过程的出参并显示出来,详细代码注释,希望对用到C#调用存储过程的小伙伴有帮助 ...
c# winform ListView实现图片浏览
winform上传图片到数据库.pdf