The operation of the image in the database

xiaoxiao2021-03-06  98

///

/// Write the image file /// private void uploadImagefile (fileInfo obj) {TRY {// to store image files. (The length of the array) Is the length of the image file) Byte [] content = new byte [obj.length]; // Open the file and use him to initialize a file stream object filestream imageFileStream = Obj.Openread (); // Write the contents of the file content in the byte array imageFileStream .Read (Content, 0, Content.length); // Close the file flow imagefilestream.close ();

// Create a Sql Command object is used to insert records SqlCommand Command = new SqlCommand ( "INSERT Into Images (Description, ImageFile, ImageSize, ImageType) Values ​​(@ Description, @ ImageFile, @ ImageSize, @ ImageType)", this.MySqlCon );

// description of the image information SqlParameter imageDescriptionParameter = new SqlParameter ( "@ Description", SqlDbType.NVarChar); imageDescriptionParameter.Value = this.txtImage.Text; Command.Parameters.Add (imageDescriptionParameter);

// Image of data byte arrays SQLParameter ImageFileParameter = New SqlParameter ("@ imagefile", sqldbtype.image; imagefileparameter.value = content; command.parameters.add (imagefileparameter);

// Image Size Information SqlParameter ImageSizeParameter = New Sqlparameter ("@ imagesize", sqldbtype.int); imagesizeParameter.Value = content.length; command.Parameters.Ad (ImageSizeParameter);

// new type of image information SqlParameter imageTypeParameter = new SqlParameter ( "@ ImageType", SqlDbType.NVarChar); imageTypeParameter.Value = obj.Extension; Command.Parameters.Add (imageTypeParameter); // Open Database Connectivity this.MySqlCon.Open (); // Execute SQL statement command.executenonQuery (); // Turn off the database connection this.mysqlcon.close ();

Messagebox.show ("Image file" obj.fullname "successfully uploaded to the database!");} Catch (exception ezone) {messagebox.show (ezone.Message);}} ///

/// database read image /// private void btnDownLoad_Click (object sender, System.EventArgs e) {try {// create a command object Sql, to query record SqlCommand command = new SqlCommand ( "select * from dazzle_pics where picid = 71605 order by picid DESC ", this.MySqlCon); // open the database connection this.MySqlCon.Open (); // execute Sql statement SqlDataReader MyReader = Command.ExecuteReader (CommandBehavior.CloseConnection); myReader.Read (); // Create a memory flow object and use the ImageFile field, iMagesize field initialize his memorystream mymemorystream = new memoryStream ((byte []) MyReader ["Source"], 0, Convert.Toint32 (MyReader ["picsize"]); // creating an Image object, and assigned to the object Picture Image property this.ImgPictureBox.Image = Image.FromStream (MyMemoryStream); // Close the memory stream MyMemoryStream.Close (); myReader.Close ();} catch (Exception Ezone) { Messagebox.show (ezone.Message);}}

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

New Post(0)