All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
开源MVC框架PixelDragonsMVC.NET发布_.net资料_编程技术
asp程序页面出现超时已过期问题_mssql学习_编程技术
C#中Windows通用的回车转Tab方法_[Asp.Net教程]
GridView中绑定日期字段格式的定义_[Asp.Net教程]
PHP和MySQL基础教程(二)_[PHP教程]
ASP.NET底层架构探索之HttpHandlers_.net资料_编程技术
动态网页PHP编程中八种常见的文件操作方式_php资料_编程技术
Asp.net获得日历控件当前选择显示的年月_.net资料_编程技术
ASP.NET2.0的multiview和wizard控件_.net资料_编程技术
几个Delphi鼠标处理事件的使用实例
Reading number is top 10 pictures
Breasts woman big set 1
Born After 90 Beijing sports university campus flower photos4
Ashlynn Brooke photograph of a group2
人造器官和铁肺人
29 the belle stars after bath figure5
擦地板的大叔太好了
2012 national geographic daily picture3
Absolutely shocked. National geographic 50 animal photographys7
The money of more than 100 countries and regions3
Sora aoi mirror memorial classics5
Download software ranking
电车之狼R
Boxer's Top ten classic battle7
在线棋牌游戏3.05版
Eclipse 4.2.2 For Win64
VC++6.0简体中文版
Unix video tutorial5
Twenty piece of palm leaf
虚拟机汉化软件
电脑知识及技巧大合集
Tram sex maniac 2 (H) rar bag8
delv published in(发表于) 2014/1/8 7:04:32 Edit(编辑)
通过ADO.NET存取文件_[Asp.Net教程]

通过ADO.NET存取文件_[Asp.Net教程]

通过ADO.NET存取文件_[Asp.Net教程]

时我们需要把一些大的数据对象如图片、可执行文件、视频和文档等数据存入数据库。在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
{
///


/// Form1 的摘要说明。
///

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;
///
/// 必需的设计器变量。
///

private System.ComponentModel.Container components = null;


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


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


///


/// 清理所有正在使用的资源。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


#region Windows 窗体设计器生成的代码
///


/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

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


///


/// 应用程序的主入口点。
///

[STAThread]
static void Main()
{
Application.Run(new ADO_Demo());
}


///


/// 点击打开文件对话框确定按钮,将文件保存到数据库中
///

///
///
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();
}


///


/// 从数据库中读取bitmap图片并显示
///

///
///
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();
}
}


///


/// 读取文件并保存到硬盘,然后打开文件
///

///
///
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();
}
}
}
}


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


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.