Take a Word file in SQL Server!

xiaoxiao2021-03-06  91

/ * How to store the Word into the SQLServer database and read it when you read it. Create TableCreate Table MyImages (Sno Int, Imgfield Image); establish C # project, add a reference (Reference-> add-> brower-> office Install the folder -> office11-> msword.ole, then don't have to ask, wait ..., then copy the code, see the details * / using system; use system.drawing; using system.collections; using system .Componentmodel; using system.data; using system.data.sqlclient; using system.io;

namespace writeFile {///

/// Summary description for Form1 /// 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; /// /// Required designer variable /// private System.ComponentModel. .Container components = null;

Public form1 () {/// Required for Windows Form Designer Support // InitializationComponent ();

// // Todo: add any constructor code after initializationcomponent call //}

///

/// clean up any resources being used. /// protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {components.dispose () }} Base.Dispose (Disposing);

#region Windows Form Designer generated code ///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor /// 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.TextBox 1. 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

///

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

Private void button1_click (Object sender, system.eventargs e) // stores Word to SQLServer {dbmana DM = New dbmana (); SqlConnection Con = DM.GetCon (); // Additional classes, return SQLServer connection, use Establish your own connection

SqlDataAdapter comm = new SqlDataAdapter ( "select * from MyImages", con); SqlCommandBuilder scb = new SqlCommandBuilder (comm); // only way to ensure dataadapter can 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)); // reads to 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. ipen () DS.Tables [0] .ROWS [0] ["content"] = data;} catch (exception ex) {this.textbox1.text = ex.toT7tring ();

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 forking, ref forking; ap.visible = true;

Private void button3_click (object sender, system.eventargs e) // read from table, and temporary storage, easy to read, then you can delete {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 (); // Note this sentence 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.toT7tring ();

Finally {con.close (); fs.close ();

}}}

转载请注明原文地址:https://www.9cbs.com/read-96094.html

New Post(0)