Interaction between a simple database and a client using binary images and binary PDF

Source: Internet
Author: User

 

This article describes three points:

1. Interaction between the binary image database and the client

2. Interaction between the binary PDF database and the client

3. A simple solution to open PDF using winform

 

Did you resign a few days ago? I went to the bookstore and read a book. I thought it was very easy to see the Binary Image Upload.

Both images and PDF files can be displayed in the winfomr form and can be downloaded to the local device.

The image and PDF fields stored in the database are of the image type.

1. Interaction between the binary image database and the client (download to a local location, refer to the PDF example)

Private void button#click (Object sender, eventargs e) {try {// The URL does not support only local files. String S = @ "E: \ mycode \ erjinzhi \ image \ youcai.jpg "; filestream F = new filestream (S, filemode. open, fileaccess. read); binaryreader B = new binaryreader (f); byte [] byteimage = B. readbytes (INT) F. length); // save it to the database sqldbtype. image Type sqlconnection conn = new sqlconnection ("Server = .; uid = sa; Password = sa; database = test1 "); sqlcommand COM = new sqlcommand (); COM. connection = conn; COM. commandtype = commandtype. text; COM. commandtext = "insert into [user] values (@ byteimage)"; COM. parameters. add ("@ byteimage", sqldbtype. image ). value = byteimage; Conn. open (); int result = com. executenonquery (); Conn. close (); If (result> 0) {COM = new sqlcommand (); COM. connection = conn; COM. commandtext = "select image from [user] Where id = @ ID"; COM. commandtype = commandtype. text; COM. parameters. add ("@ ID", sqldbtype. INT ). value = 1; Conn. open (); object OBJ = com. executescalar (); Conn. close (); byteimage = (byte []) OBJ; memorystream M = new memorystream (byteimage); // set image picturebox2.image = image for the image control. fromstream (m) ;}} catch (exception ex) {MessageBox. show (ex. message. tostring ());}}

2. Interaction between the binary PDF database and the client (see the next instance when the interface is displayed)

// You can assign the local address to ax1_1_1.src after downloading it locally.

Private void button2_click (Object sender, eventargs E) {try {// If the PDF file is too large, you need to set the database timeout time to a greater value. I tried to set a timeout error for the PDF file of MB. // If the PDF file is encrypted, it seems that an error is returned. If the PDF file is not in the format of string S = @ "F: \ Desktop \ firebug-firefox_firebug_ .pdf "; filestream F = new filestream (S, filemode. open, fileaccess. read); binaryreader B = new binaryreader (f); byte [] bytepdf = B. readbytes (INT) F. length); // save it to the database sqldbtype. image Type sqlconnection conn = new sqlconnection ("Server = .; uid = sa; Password = sa; database = test1 "); sqlcommand COM = new sqlcommand (); COM. connection = conn; COM. commandtype = commandtype. text; COM. commandtext = "insert into [user] values (@ bytepdf)"; COM. parameters. add ("@ bytepdf", sqldbtype. image ). value = bytepdf; Conn. open (); int result = com. executenonquery (); Conn. close (); If (result> 0) {COM = new sqlcommand (); COM. connection = conn; COM. commandtext = "select image from [user] Where id = @ ID"; COM. commandtype = commandtype. text; COM. parameters. add ("@ ID", sqldbtype. INT ). value = 6; Conn. open (); object OBJ = com. executescalar (); Conn. close (); // PDF read the binary in the database and then report an error to the local // reference: http://topic.csdn.net/u/20080415/11/048deb7e-b0c2-4a20-94a5-88be74ce47f0.html bytepdf = (byte []) OBJ; string filepath = @ "E: \ xxx.pdf "; filestream FS; If (file. exists (filepath) {FS = new filestream (filepath, filemode. truncate);} else {FS = new filestream (filepath, filemode. createnew);} binarywriter BR = new binarywriter (FS); BR. write (bytepdf, 0, bytepdf. length); BR. close (); FS. close (); process. start (filepath) ;}} catch (exception ex) {MessageBox. show (ex. message. tostring ());}}

3.a simple solution for displaying data in winform in PDF

// View PDF in winform // open toolbox // right-click Select item com build select Adobe PDF Reader // set PDF address SRC // http://dingxq.blog.163.com/blog/static/13052576120108805924575/ ax1_1_1.src = @ "E: \ xxx.pdf ";

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.