Using C # in SQL Server2000 access image (extracted)

zhaozj2021-02-16  64

// SQL server2000 saved image display Picture buffByte = null in the private void listBox1_SelectedIndexChanged (object sender, System.EventArgs e) {byte []; string comm = @ "select img from table1 where id =" this.listBox1 .SelectedValue; this.sqlCommand1 = new System.Data.SqlClient.SqlCommand (); this.sqlCommand1.CommandType = System.Data.CommandType.Text; this.sqlCommand1.CommandText = comm; this.sqlCommand1.Connection = this.sqlConnection1; THIS.SQLCONNECTION1.Open (); system.data.sqlclcent.sqlDataReader rd = this.sqlcommand1.executeReader (); while (rd.read ()) {buffbyte = ((byte []) RD [0]);} rd .Close (); this.sqlConnection1.close (); // put the image array array into memory current system.io.MemoryStream ms = new system.io.MemoryStream (BUFFBYTE); // Establish BitMap System through the stream .Drawing.Bitmap bmp = new Bitmap (ms); this.pictureBox1.Image = bmp;} // save the image to the image field SQL server2000 private void button2_Click_1 (object sender, System.EventArgs e) {string pathName; if (this.openfiledialog1.showdia log () == System.Windows.Forms.DialogResult.OK) {pathName = this.openFileDialog1.FileName; System.Drawing.Image img = System.Drawing.Image.FromFile (pathName); this.pictureBox1.Image = img; // Read the image into byte arrays System.IO.FileStream fs = new system.io.filestream (pathname, system.io.filemode.open, system.io.filecess.read; byte [] buffbyte = new Byte [fs.length]; fs.read (buffbyte, 0, (int) fs.length); fs.close (); fs = null; // Established Command Command String Comm = @ "INSERT INTO TABLE1 (IMG, NAME) VALUES (@ img, @ name) "; this.sqlcommand1 = new system.data.sqlclient.sqlcommand ();

this.sqlCommand1.CommandType = System.Data.CommandType.Text; this.sqlCommand1.CommandText = comm; this.sqlCommand1.Connection = this.sqlConnection1; // create Parameter this.sqlCommand1.Parameters.Add ( "@ img", System .Data.sqldbtype.Image); this.sqlcommand1.parameters [0] .value = buffbyte; this.sqlcommand1.parameters.add ("@ name", system.data.sqldbtype.varchar); this.sqlcommand1.parameters [1 ] .Value = pathname.substring (Pathname.lastIndexof ("//") 1

); Try {this.sqlConnection1.open (); this.sqlcommand1.executenonQuery (); this.sqlconnection1.close ();} catch (system.exception ee) {messagebox.show (ee.MESSAGE);} buffbyte = null ;

This.FillListBox ();}}

Original source: http://blog.codelphi.com/miracle/Articles/6053.aspx? Pending = true

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

New Post(0)