首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 软件教学-> 图文处理
通过ado.net存取文件_ado.net应用
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

有时我们需要把一些大的数据对象如图片、可执行文件、视频和文档等数据存入数据库。在MS SQL Server中,这要用到Image数据类型,可以保存多达2G的数据。以下给出一个通过ADO.NET和MS SQL Server实现的小小的例子。

先创建一个测试数据表。
在查询分析器中输入并执行以下语句:
Create table [imgtable](
 [imgid] [int] IDENTITY(1,1) NOT NULL,
 [imgname] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL,
 [imgData] [image] NULL,
 PRIMARY KEY CLUSTERED
 (
 [imgid]
 ) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

这要在你所选的数据库中就多了一个名叫imgtable的表。

VS中的代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace ADO_Demo
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class ADO_Demo : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.Windows.Forms.Button button3;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public ADO_Demo()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.pictureBox1 = new System.Windows.Forms.PictureBox();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.button3 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(368, 48);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(104, 23);
   this.button1.TabIndex = 0;
   this.button1.Text = "保存图片";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(368, 120);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(104, 23);
   this.button2.TabIndex = 1;
   this.button2.Text = "显示图片";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // pictureBox1
   //
   this.pictureBox1.Location = new System.Drawing.Point(8, 16);
   this.pictureBox1.Name = "pictureBox1";
   this.pictureBox1.Size = new System.Drawing.Size(312, 288);
   this.pictureBox1.TabIndex = 2;
   this.pictureBox1.TabStop = false;
   //
   // openFileDialog1
   //
   this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(368, 200);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(104, 23);
   this.button3.TabIndex = 1;
   this.button3.Text = "读取文件并打开";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // ADO_Demo
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(496, 317);
   this.Controls.Add(this.pictureBox1);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.button3);
   this.Name = "ADO_Demo";
   this.Text = "ADO_Demo";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new ADO_Demo());
  }

  /// <summary>
  /// 点击打开文件对话框确定按钮,将文件保存到数据库中
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
  {
   string filename = this.openFileDialog1.FileName;
   SqlConnection conn = new SqlConnection("server=192.168.2.200;integrated security = sspi;database = northwind");
   SqlCommand cmd = new SqlCommand("insert imgtable values(@imgname,@imgData)",conn);
   SqlParameter pm = new SqlParameter("@imgname",SqlDbType.VarChar,100);
   pm.Value = filename;
   SqlParameter pm1 = new SqlParameter("@imgData",SqlDbType.Image);
   FileStream fs = new FileStream(filename,FileMode.Open);
   int len = (int)fs.Length;
   byte[] fileData = new byte[len];
   fs.Read(fileData,0,len);
   fs.Close();

   pm1.Value = fileData;
   cmd.Parameters.Add(pm);
   cmd.Parameters.Add(pm1);

   conn.Open();
   try
   {
    cmd.ExecuteNonQuery();
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }


  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   this.openFileDialog1.ShowDialog();
  }

  /// <summary>
  /// 从数据库中读取bitmap图片并显示
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button2_Click(object sender, System.EventArgs e)
  {
   SqlConnection conn = new SqlConnection("server=192.168.2.200;integrated security = sspi;database = northwind");
   SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like %bmp%",conn);
   conn.Open();
   SqlDataReader dr;
   try
   {
    dr = cmd.ExecuteReader();
    dr.Read();
    System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
    //或byte[] imageData = (byte[])dr[2];
    MemoryStream ms = new MemoryStream(sb.Value);//在内存中操作图片数据
    Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
    this.pictureBox1.Image = bmp;
    dr.Close();
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   finally
   {
    conn.Close();
   }
  }

  /// <summary>
  /// 读取文件并保存到硬盘,然后打开文件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button3_Click(object sender, System.EventArgs e)
  {
   SqlConnection conn = new SqlConnection("server=192.168.2.200;integrated security = sspi;database = northwind");
   SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like %doc",conn);
   conn.Open();
   SqlDataReader dr;
   try
   {
    dr = cmd.ExecuteReader();
    dr.Read();
    System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
    //或byte[] imageData = (byte[])dr[2];
    //FileStream fs = new FileStream(@"C:\temp.bmp",FileMode.Create);
    string filename = @"C:\" + System.IO.Path.GetFileName(dr.GetString(1));
    FileStream fs = new FileStream(filename,FileMode.Create);
    fs.Write(sb.Value,0,sb.Value.Length);
    fs.Close();
    //this.pictureBox1.Image = Image.FromFile(@"C:\temp.bmp");
    System.Diagnostics.Process.Start(filename);
    dr.Close();
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   finally
   {
    conn.Close();
   }
  }
 }
}

直接把整个文件读取到内存中的数组里对于小文件来说是没问题的,但如果是大文件,特别是大小都超过了物理内存的文件,可能会导致严重的内存问题,需要分段读取,并分段写到数据库。

http://maxianghui.cnblogs.com/archive/2006/07/12/448943.html


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Java Reflection (JAVA反射)-JSP教程,Java技巧及代码
·WebServices入门-.NET教程,Web Service开发
·如何在c#中使用mapx5.0(二)-.NET教程,C#语言
·用java启动记事本程序,并输入内容-JSP教程,Java技巧及代码
·正则表达式-ASP教程,正则表达式
·sql server中的外键约束-数据库专栏,SQL Server
·java中的基于套结字(socket)的通信 一. 一个双人单方向通信例子-JSP教程,Java技巧及代码
·图解利用Eclipse3+Lomboz3+Tomcat开发JSP --1.环境-JSP教程,Jsp/Servlet
·EditPlus+NAnt构建轻巧的.NET开发环境-.NET教程,Asp.Net开发
·ASP.NET2.0下利用javascript实现TreeView中的checkbox全选-.NET教程,Asp.Net开发
最新文章
·photoshop简单为美女照片瘦身及调色_photoshop教程
·fireworks巧绘精美可爱水杯图标_fireworks教程
·如何修复windows vista系统用户损坏的配置文件_windows vista
·本地门户营运的3个重点注意事项_站长心得
·必知:在线推广网站的十四种方法_站长心得
·google analytics统计分析新版本说明_google推广
·google adsense 违规行为集_google推广
·googleadsense的无效点击_google推广
·关于网络原创作品的版权问题_网络编辑
·符合xhtml标准div+css布局的网站对网站seo的影响_seo网站优化
相关主题
  • 通过ado.net实现事务处理_ado.net应用
  • 通过ADO调用Access数据库和COM程序-PHP教程,数据库相关
  • 通过ADO.NET访问数据库-.NET教程,数据库应用
  • 通过ADO连接各种数据库的字符串翠集(二)(完)-ASP教程,数据库相关
  • 通过ADO连接各种数据库的字符串翠集(一)-ASP教程,数据库相关
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭