Accessing BLOB fields in Oracle under. Net (2) upload and download

Source: Internet
Author: User

Upload

It is similar to the previous article, but the text format is inserted first. This is a small use case for blob. Now upload the attachment code:

 

1 // Add a file --------- save the file to the database
2 Public void attach (string filename, string FN, string username, String title)
3 {users. class1 u = new users. class1 (); // obtain the user ID
4 int uid = U. getuserid (username );
5 papers. Papers P = new papers ();
6 int paper_id = P. getpaperid (title, UID); // obtain the paper ID.
7 string SQL = "Update paper_table set attach =: AA, attachfilename =: BB where id = '" + paper_id + "'";
8 oraclecommand cmd = new oraclecommand (SQL, Conn );
9 conn. open ();
10 filestream FS = file. openread (filename );
11 byte [] B = new byte [fs. Length];
12 FS. Read (B, 0, B. Length );
13 FS. Close ();
14
15 cmd. Parameters. Add ("AA", system. Data. oracleclient. oracletype. bfile, B. Length). value = B;
16 cmd. Parameters. Add ("BB", system. Data. oracleclient. oracletype. varchar). value = FN;
17 cmd. executenonquery ();
18 conn. Close ();
19}

 

Download

First, convert the downloaded binary data to the format previously stored in the database. That is, the file extension must be saved when the file is saved.

The converted file is stored in a temporary folder, and then you can directly download response. Redirect.

 

1 // download the file-collected on the internet is just an annotation-the following is for the Oracle database, the same as for SQL Server
2 Public String download (string PID, string path)
3 {
4
5 filestream objfs;
6
7 binarywriter objbw;
8
9 int buffersize = 260000; // This is the buffer size. The value is too small. If the value is smaller than the downloaded file size, an error occurs.
10
11 byte [] outbyte = new byte [buffersize];
12
13 long retval;
14
15 long startindex = 0;
16
17 string filename = "";
18
19 string SQL = "select * From paper_table where id = '" + PID + "'"; // obtain from database
20 oraclecommand cmd = new oraclecommand (SQL, Conn );
21 conn. open ();
22 oracledatareader DR = cmd. executereader (commandbehavior. sequentialaccess );
23 dr. Read ();
24
25 filename = Dr ["attachfilename"]. tostring (); // file name
26 string tempfilename = filename;
27 filename = path + filename;
28 objfs = new filestream (filename, filemode. openorcreate, fileaccess. Write );
29
30 objbw = new binarywriter (objfs );
31
32 startindex = 0;
33
34 retval = dr. getbytes (8, startindex, outbyte, 0, buffersize );
35
36 while (retval = buffersize)
37 {
38 objbw. Write (outbyte );
39
40 objbw. Flush ();
41
42 startindex + = buffersize;
43
44 retval = dr. getbytes (8, startindex, outbyte, 0, buffersize );
45}
46
47 objbw. Write (outbyte, 0, (INT) retval-1 );
48
49 objbw. Flush ();
50
51 objbw. Close ();
52
53 objfs. Close ();
54
55 dr. Close ();
56
57 conn. Close ();
58 return tempfilename;
59
60
61
62
63
64
65
66}
67
Related Article

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.