ASP. NET code for writing and reading IMAGE fields in MSSQL

Source: Internet
Author: User
Today, I met a project on an enterprise website. The customer asked the product data table in the system to use the database in their company's ERP, the product images can also be changed (the ERP product images are stored in the IMAGE field). If there is no replaceable IMAGE, then we can directly read the source IMAGE inserted by the ERP in the product table. After finding the source IMAGE, we can find a feasible code to read the IMAGE field and display the IMAGE in the browser:

C # code
  1. Try
  2. {
  3. Byte [] imgData;
  4. ImgData = (byte []) dtbl_pd.Rows [0] ["yytp"]; // IMAGE field
  5. Int iLength = imgData. Length;
  6. // FileStream fs = new FileStream ("E: \ Project \ CompanyWebSite \ 1.gif", System. IO. fileMode. create, System. IO. fileAccess. write); // can be used to generate the actual file
  7. // Fs. Write (imgData, 0, iLength );
  8. // Fs. Close ();
  9. Response. OutputStream. Write (imgData, 0, iLength );
  10. }
  11. Catch (Exception ex)
  12. {
  13. }

 

The call is very simple. For example, if the above Code is located on the page named Img. aspx, you can insert:

In addition, it also provides an insert when reading data. It is feasible after testing.

Key code of InsertImage. aspx:

HTML code
  1. <Form enctype = "multipart/form-data" runat = "server" id = "Form1">
  2. <Table runat = "server" width = "700" align = "left" id = "Table1" cellpadding = "0" cellspacing = "0" border = "0">
  3. <Tbody>
  4. <Tr>
  5. <Td> upload an image (select the image you want to upload) </td>
  6. <Td>
  7. <Input type = "file" id = "UP_FILE" runat = "server" style = "Width: 320" accept = "text/*" name = "UP_FILE"/>
  8. </Td>
  9. </Tr>
  10. <Tr>
  11. <Td> file description (add upload image description, for example, author and source) </td>
  12. <Td>
  13. <Asp: TextBox RUNAT = "server" WIDTH = "239" ID = "txtDescription"/>
  14. </Td>
  15. </Tr>
  16. <Tr>
  17. <Td>
  18. <Asp: Label RUNAT = "server" ID = "txtMessage" FORECOLOR = "red"/>
  19. </Td>
  20. <Td>
  21. <Asp: Button ID = "Button1" RUNAT = "server" WIDTH = "239" onCLICK = "Button_Submit" TEXT = "UploadImage"/> </td>
  22. </Tr>
  23. </Tbody>
  24. </Table>
  25. </Form>

Key code of InsertImage. aspx. cs:

C # code

 
  1. Protected void Button_Submit (System. Object sender, System. EventArgs e)
  2. {
  3. HttpPostedFile UpFile = UP_FILE.PostedFile; // HttpPostedFile object, used to read image file attributes
  4. FileLength = UpFile. ContentLength; // record file length
  5. Try
  6. {
  7. If (FileLength = 0)
  8. {// The file length is zero.
  9. TxtMessage. Text = "<B> select the file you want to upload </B> ";
  10. }
  11. Else
  12. {
  13. Byte [] FileByteArray = new Byte [FileLength]; // temporary storage of Byte Arrays for image files
  14. Stream StreamObject = UpFile. InputStream; // create a data Stream object
  15. // Read image file data. FileByteArray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
  16. StreamObject. Read (FileByteArray, 0, FileLength );
  17. // Create an SQL Server Link
  18. SqlConnection Con = new SqlConnection ("Data Source = Localhost; Initial Catalog = CompanyWebSiteData; User ID = sa; Pwd = chenfeng ;");
  19. String SqlCmd = "Insert INTO cp (cpbh, yytp) valueS (@ cpbh, @ Image )";
  20. SqlCommand CmdObj = new SqlCommand (SqlCmd, Con );
  21. CmdObj. Parameters. Add ("@ Image", SqlDbType. Binary, FileLength). Value = FileByteArray;
  22. CmdObj. Parameters. Add ("@ cpbh", SqlDbType. VarChar, 200). Value = txtDescription. Text;
  23. //// Upload the data records of other single tables
  24. // CmdObj. Parameters. Add ("@ ImageDescription", SqlDbType. VarChar, 200). Value = txtDescription. Text;
  25. //// Record the file length, which is used for reading
  26. // CmdObj. Parameters. Add ("@ ImageSize", SqlDbType. BigInt, 8). Value = UpFile. ContentLength;
  27. Con. Open ();
  28. CmdObj. ExecuteNonQuery ();
  29. Con. Close ();
  30. TxtMessage. Text = "<p> <B> OK! You have successfully uploaded your image </B> "; // a message indicating that the image has been uploaded successfully
  31. }
  32. }
  33. Catch (Exception ex)
  34. {
  35. TxtMessage. Text = ex. Message. ToString ();
  36. }
  37. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.