Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146608
  • 博文数量: 58
  • 博客积分: 1584
  • 博客等级: 上尉
  • 技术积分: 605
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-12 10:06
文章分类

全部博文(58)

文章存档

2011年(7)

2010年(51)

我的朋友

分类: 数据库开发技术

2010-08-10 17:12:59

用c#向SQL Server中存储图片并且再从数据库中读取图片

(2010-07-09 18:25:47)
标签:

杂谈

在WinForm中,使C#语言向SQL Server(2005)存储图片并且将图片从数据库中读取出来,显示在窗体中
步骤:
1.首先,在SQL Server中创建一个表,用来存储图片;
示例代码:
use tempdb
go
if exists (select * from sysobjects where name = 'Images')
drop table Images
go
create table Images
(
 BLODID int identity not null,
 BLOBData image not null
)

2.打开Visual Studio2005,创建一个WinForm应用程序。向Form1中添加一个PictureBox控件,再添加两个

Button控件,将Button1的Text属性分别设为"保存图片","显示图片";

3.在Form1的代码之中,首先引入命名空间:
    using System.Data.SqlCilent;
    using System.IO;
    using System.Drawing.Imaging;

4.编写"保存图片"按钮的单击事件,用于保存图片;
示例代码:
private void button1_Click(object sender, EventArgs e)
        {
           
            try
            {
                string strConn = "Server=.;DataBase=tempdb;Integrated Security=true";
                SqlConnection connection = new SqlConnection(strConn);
                string sql = "insert into blobtest (blobdata) values (@blobdata)";
                SqlCommand command = new SqlCommand(sql, connection);
                //图片路径
                string picturePath = @"D:\pic.jpg";  //注意,这里需要指定保存图片的绝对路径和图片 

                                               //文件的名称,每次必须更换图片的名称,这里很为不便
                //创建FileStream对象
                FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
                //声明Byte数组
                Byte[] mybyte = new byte[fs.Length];
                //读取数据
                fs.Read(mybyte,0,mybyte.Length);
                fs.Close();
                //转换成二进制数据,并保存到数据库
                SqlParameter prm = new SqlParameter
("@blobdata",SqlDbType.VarBinary,mybyte.Length,ParameterDirection.Input,false,0,0,null,DataRowVers

ion.Current,mybyte);
                command.Parameters.Add(prm);
                //打开数据库连接
                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

5.编写"显示图片"的单击事件,将图片从数据库中读取出来显示在PictureBox之中
示例代码:
private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //创建数据库连接字符串
                string strConn = "Server=.;DataBase=tempdb;Integrated Security=True";
                //创建SqlConnection对象
                SqlConnection connection = new SqlConnection(strConn);
                //打开数据库连接
                connection.Open();
                //创建SQL语句
                string sql = "select blodid,blobdata from blobtest order by blodid";
                //创建SqlCommand对象
                SqlCommand command = new SqlCommand(sql,connection);
                //创建DataAdapter对象
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                //创建DataSet对象
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet, "BLOBTest");
                int c = dataSet.Tables["BLOBTest"].Rows.Count;
                if (c > 0)
                {
                    Byte[] mybyte = new byte[0];
                    mybyte = (Byte[])(dataSet.Tables["BLOBTest"].Rows[c-1]["BLOBData"]);
                    MemoryStream ms = new MemoryStream(mybyte);
                    pictureBox1.Image = Image.FromStream(ms);
                }
                connection.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

这样就可以实现用c#语言向SQL Server中保存图片,并从数据库中读取图片显示在窗体之中了···

阅读(1878) | 评论(0) | 转发(0) |
0

上一篇:MVC

下一篇:ASP.NET MVC框架(第一部分)

给主人留下些什么吧!~~