怎么样把word文件保存到sql数据库里面

1 view
Skip to first unread message

goag...@gmail.com

unread,
May 7, 2006, 9:17:41 PM5/7/06
to goagrass
两种方法:
1 把word文件转换成流的形式保存在数据库中;
2 在数据库中只保存word文件的相应存取路径;

优缺点:
1 文件管理方便,但存取速度较慢;
2 文件存取速度快(硬盘直接读取),但文件管理不方便;


1.使用流的形式
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;


namespace regedit
{
/// <summary>
/// Picture 的摘要描述。
/// </summary>
public class Picture : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDlg;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.TextBox txtfile;
private System.Windows.Forms.Button btnInsert;
private System.Windows.Forms.TextBox txtname;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;

/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.Container components = null;
SqlConnection conn=new SqlConnection("Data Source=localhost;
Integrated Security=SSPI;Initial Catalog=mis");
public Picture()
{
//
// Windows Form 設計工具支援的必要項
//
InitializeComponent();

//
// TODO: 在 InitializeComponent
呼叫之後加入任何建構函式程式碼
//
}

/// <summary>
/// 清除任何使用中的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所必須的方法 -
請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.openFileDlg = new System.Windows.Forms.OpenFileDialog();
this.label1 = new System.Windows.Forms.Label();
this.txtfile = new System.Windows.Forms.TextBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.btnInsert = new System.Windows.Forms.Button();
this.txtname = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 0;
this.label1.Text = "ImageName";
//
// txtfile
//
this.txtfile.Location = new System.Drawing.Point(88, 16);
this.txtfile.Name = "txtfile";
this.txtfile.Size = new System.Drawing.Size(384, 22);
this.txtfile.TabIndex = 1;
this.txtfile.Text = "";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(24, 48);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.TabIndex = 2;
this.btnBrowse.Text = "Browse";
this.btnBrowse.Click += new
System.EventHandler(this.btnBrowse_Click);
//
// btnInsert
//
this.btnInsert.Location = new System.Drawing.Point(112, 48);
this.btnInsert.Name = "btnInsert";
this.btnInsert.TabIndex = 3;
this.btnInsert.Text = "Insert";
this.btnInsert.Click += new
System.EventHandler(this.btnInsert_Click);
//
// txtname
//
this.txtname.Location = new System.Drawing.Point(112, 88);
this.txtname.Name = "txtname";
this.txtname.TabIndex = 4;
this.txtname.Text = "txtname";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 88);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(24, 128);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(320, 160);
this.pictureBox1.TabIndex = 6;
this.pictureBox1.TabStop = false;
//
// Picture
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(488, 317);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtname);
this.Controls.Add(this.btnInsert);
this.Controls.Add(this.btnBrowse);
this.Controls.Add(this.txtfile);
this.Controls.Add(this.label1);
this.Name = "Picture";
this.Text = "操作圖片";
this.Load += new System.EventHandler(this.Picture_Load);
this.ResumeLayout(false);

}
#endregion

private void btnBrowse_Click(object sender, System.EventArgs e)
{
if (openFileDlg.ShowDialog()==DialogResult.OK )
{
txtfile.Text=openFileDlg.FileName;
btnInsert.Enabled = true;
}
}

private void btnInsert_Click(object sender, System.EventArgs e)
{
FileStream fs=File.OpenRead(txtfile.Text);
byte[] content=new byte[fs.Length];
fs.Read(content, 0,content.Length);
fs.Close();
conn.Open();
string sql ="insert into Photos(name,Photo) values(@name, @Photos)";
SqlCommand comm=new SqlCommand(sql,conn);
comm.Parameters.Add("@Photos", SqlDbType.Image).Value=content;
comm.Parameters.Add("@name", SqlDbType.NVarChar).Value=txtname.Text;
if(comm.ExecuteNonQuery()==1)
{
MessageBox.Show("Successfully insert image into database!");
}
else
{
MessageBox.Show("Failed to insert image into database");
}
conn.Close();
}

private void Picture_Load(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
ShowImage(txtname.Text);
}

private void ShowImage(string s)
{
string str = "SELECT photo FROM Photos WHERE name='" + s +"'";
SqlCommand cmd = new SqlCommand(str, conn);
conn.Open();
byte[] b= (byte[])cmd.ExecuteScalar();
if (b.Length > 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
DrawToScale(new Bitmap(stream));
stream.Close();
}
conn.Close();
}

private void DrawToScale(Image bmp)
{
pictureBox1.Image = new Bitmap(bmp);
}

}
}

2.使用路径,数据库里保存路径,读的时候根据路径找到文件

参看
http://blog.csdn.net/knight94/archive/2006/03/24/637800.aspx

Reply all
Reply to author
Forward
0 new messages