国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

通過ADO.NET存取文件

2019-11-18 17:06:39
字體:
供稿:網(wǎng)友

有時我們需要把一些大的數(shù)據(jù)對象如圖片、可執(zhí)行文件、視頻和文檔等數(shù)據(jù)存入數(shù)據(jù)庫。在MS SQL Server中,這要用到Image數(shù)據(jù)類型,可以保存多達2G的數(shù)據(jù)。以下給出一個通過ADO.NET和MS SQL Server實現(xiàn)的小小的例子。

先創(chuàng)建一個測試數(shù)據(jù)表。
在查詢分析器中輸入并執(zhí)行以下語句:
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]

這要在你所選的數(shù)據(jù)庫中就多了一個名叫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>
  /// 必需的設(shè)計器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public ADO_Demo()
  {
   //
   // Windows 窗體設(shè)計器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }

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

  #region Windows 窗體設(shè)計器生成的代碼
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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>
  /// 應(yīng)用程序的主入口點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   application.Run(new ADO_Demo());
  }

  /// <summary>
  /// 點擊打開文件對話框確定按鈕,將文件保存到數(shù)據(jù)庫中
  /// </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>
  /// 從數(shù)據(jù)庫中讀取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);//在內(nèi)存中操作圖片數(shù)據(jù)
    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();
   }
  }
 }
}

直接把整個文件讀取到內(nèi)存中的數(shù)組里對于小文件來說是沒問題的,但如果是大文件,特別是大小都超過了物理內(nèi)存的文件,可能會導致嚴重的內(nèi)存問題,需要分段讀取,并分段寫到數(shù)據(jù)庫。

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 吴江市| 台安县| 资中县| 吴旗县| 册亨县| 抚松县| 任丘市| 衡东县| 司法| 金乡县| 土默特左旗| 乐陵市| 毕节市| 丰原市| 衡水市| 西吉县| 囊谦县| 沙坪坝区| 衡南县| 雷山县| 磐石市| 潼关县| 五大连池市| 海林市| 奉新县| 叙永县| 山西省| 武清区| 辽阳市| 宁安市| 和硕县| 屏东市| 沂南县| 嘉义县| 鹰潭市| 左贡县| 乐亭县| 五常市| 来安县| 嘉祥县| 永和县|