把word存入sqlserver

/*如何把word存入sqlserver数据库,并且读取的时候还是用word读取.
建立table
create table myimages(sno int,imgfield image);
建立c#工程,添加引用(reference->add->brower->office安装文件夹->office11->msword.ole,然后什么都不用问了,等待...,然后拷贝下代码,具体功能见注释*/
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 writefile
{
 /// <summary>
 /// summary description for form1.
 /// </summary>
 public class form1 : system.windows.forms.form
 {
  private system.windows.forms.button button1;
  private system.windows.forms.textbox textbox1;
  private system.windows.forms.button button2;
  private system.windows.forms.button button3;
  /// <summary>
  /// required designer variable.
  /// </summary>
  private system.componentmodel.container components = null;

  public form1()
  {
   //
   // required for windows form designer support
   //
   initializecomponent();

   //
   // todo: add any constructor code after initializecomponent call
   //
  }

  /// <summary>
  /// clean up any resources being used.
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null) 
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region windows form designer generated code
  /// <summary>
  /// required method for designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void initializecomponent()
  {
   this.button1 = new system.windows.forms.button();
   this.textbox1 = new system.windows.forms.textbox();
   this.button2 = new system.windows.forms.button();
   this.button3 = new system.windows.forms.button();
   this.suspendlayout();
   // 
   // button1
   // 
   this.button1.location = new system.drawing.point(576, 320);
   this.button1.name = "button1";
   this.button1.size = new system.drawing.size(112, 23);
   this.button1.tabindex = 0;
   this.button1.text = "savefiletodb";
   this.button1.click += new system.eventhandler(this.button1_click);
   // 
   // textbox1
   // 
   this.textbox1.location = new system.drawing.point(0, 0);
   this.textbox1.multiline = true;
   this.textbox1.name = "textbox1";
   this.textbox1.size = new system.drawing.size(568, 424);
   this.textbox1.tabindex = 1;
   this.textbox1.text = "textbox1";
   // 
   // button2
   // 
   this.button2.location = new system.drawing.point(600, 104);
   this.button2.name = "button2";
   this.button2.tabindex = 2;
   this.button2.text = "readword";
   this.button2.click += new system.eventhandler(this.button2_click);
   // 
   // button3
   // 
   this.button3.location = new system.drawing.point(568, 184);
   this.button3.name = "button3";
   this.button3.size = new system.drawing.size(112, 23);
   this.button3.tabindex = 3;
   this.button3.text = "readfromdb";
   this.button3.click += new system.eventhandler(this.button3_click);
   // 
   // form1
   // 
   this.autoscalebasesize = new system.drawing.size(5, 13);
   this.clientsize = new system.drawing.size(712, 429);
   this.controls.add(this.button3);
   this.controls.add(this.button2);
   this.controls.add(this.textbox1);
   this.controls.add(this.button1);
   this.name = "form1";
   this.text = "form1";

 this.load += new system.eventhandler(this.form1_load);
   this.resumelayout(false);


  }
  #endregion

  /// <summary>
  /// the main entry point for the application.
  /// </summary>
  [stathread]
  static void main() 
  {
   application.run(new form1());
  }

  private void button1_click(object sender, system.eventargs e)//word存入sqlserver
  {
     dbmana dm=new dbmana();
   sqlconnection con=dm.getcon();//
另外的类,返回sqlserver连接,用的时候要自己建立连接

   
   sqldataadapter comm=new sqldataadapter("select * from myimages",con);
   sqlcommandbuilder scb=new sqlcommandbuilder(comm);//
只有这样才能保证dataadapter可以update
   dataset ds=new dataset("myimages");
   comm.fill(ds,"myimages");
   comm.missingschemaaction=missingschemaaction.addwithkey;
   datarow newrow;
   newrow=ds.tables[0].newrow();
   filestream fs=new filestream(@"e:\club.doc",filemode.open,fileaccess.read);
   byte[] data=new byte[fs.length];
   fs.read(data,0,convert.toint32(fs.length));//
文件读到byte[]
   fs.close();
   comm.fill(ds,"myimages");
   newrow["id"]=3;
   newrow["description"]="this is a file";
   newrow["imgfield"]=data;//insert the binary stream
   ds.tables["myimages"].rows.add(newrow);
   comm.update(ds,"myimages");//update,then the table is update
   con.close();

   /*try
   {
    con.open();
    ds.tables[0].rows[0]["content"]=data;
   }
   catch(exception ex)
   {
    this.textbox1.text=ex.tostring();


   }
   finally
   {
    fs.close();
    con.close();

   }*/

  
  }

  private void form1_load(object sender, system.eventargs e)
  {
   
  }

  private void button2_click(object sender, system.eventargs e)//read the word file
  {
      object nothing=system.reflection.missing.value;
     object filename=@"e:\club.doc";
   word.applicationclass ap=new word.applicationclass();
      word.document doc=ap.documents.open(ref filename,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing);
   
   ap.visible=true;
      
   
  
  }

  private void button3_click(object sender, system.eventargs e)//read from table,and临时存放,便于读取然后可以删掉
  {
   dbmana dm=new dbmana();
  sqlconnection con=dm.getcon();
   //sqlcommand com=new sqlcommand("select imgfield from myimages",con);
   dataset ds=new dataset();
   sqldataadapter comm=new sqldataadapter("select imgfield from myimages",con);
   sqlcommandbuilder scb=new sqlcommandbuilder(comm);
   byte[] data=new byte[0];
      comm.fill(ds,"myimages");
   datarow newrow;
   newrow=ds.tables["myimages"].rows[3];
   data=(byte[])newrow["imgfield"];
   int arraysize=new int();//
注意这句话
   arraysize=data.getupperbound(0);

   filestream fs=new filestream(@"e:\test.doc",filemode.openorcreate,fileaccess.write);
   try
   {
    
    
    fs.write(data,0,arraysize);
   }
   catch(exception ex)
   {
    this.textbox1.text=ex.tostring();

   }
   finally
   {
    con.close();
    fs.close();
    
   }

   
   
  
  }
 }

 

posted on 2012-03-08 15:05 青蛙學堂 阅读(647) 评论(0)  编辑 收藏 引用 所属分类: 軟件布袋數據庫

只有注册用户登录后才能发表评论。
<2020年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜