/ * 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 {///
Public form1 () {/// Required for Windows Form Designer Support // InitializationComponent ();
// // Todo: add any constructor code after initializationcomponent call //}
///
#region Windows Form Designer generated code ///
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
///
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 ();
}}}