C #. Net inserts a file into the Blob field of Oracle

Source: Internet
Author: User

Freshly released code

Private Boolean importgatherfile (string blobfilefolder, string xqdm, string xqmc)
{
Try
{
System. Io. filestream FS = NULL;

String [] blobfiles = directory. getfiles (blobfilefolder );
If (blobfiles. Length = 0)
Return false;
For (INT I = 0; I <blobfiles. length; I ++)
{
// File stream
String ppath = blobfiles [I];
FS = new system. Io. filestream (ppath, filemode. Open, fileaccess. Read );
Byte [] blob = new byte [fs. Length];
FS. Read (blob, 0, blob. Length );
FS. Close ();
// Maximum ID
Int intmaxoid = getmaxoid ("tablea", "f_oid ");
// Obtain the current age
String stryear = datetime. Now. year. tostring (). substring (0, 4 );
// File name
String pfilename;
Pfilename = ppath. substring (ppath. lastindexof ("//") + 1 );

Oracleparameter Param = NULL;
Oraclecommand cmd = NULL;
Cmd = mpropconnection. createcommand (); // mpropconnection is a variable of the oracleconnection type.
Cmd. commandtext = "insert into tablea (f_oid, f_xzqhdm, f_xzqhmc, f_filename, f_file, f_year) values (" + intmaxoid + ", '" + xqdm + "', '"+ xqmc +"', '"+ pfilename +"',: Doc, '"+ stryear + "')";
Param = new oracleparameter ("Doc", oracletype. Blob, blob. length, parameterdirection. Input, false, 0, 0, null, datarowversion. Current, blob );
Cmd. Parameters. Add (PARAM );
Cmd. executenonquery ();
}
Return true;

}
Catch (exception ex)
{
Return false;
}
}

Notes

1. filestream code for reading files;

2. Command variable (CMD) parameter "; Doc" is used in Oracle: @ should be used in SQL database @;

3. In the command parameter variable (PARAM), only "Doc" is used for the parameter without the colon. As to whether to use @ in SQL, the code to be viewed at night seems to need to be added

4. the type of the command parameter variable (PARAM). oracletype. Blob indicates the inserted type. I tried to use the oledb connection to complete this function. Unfortunately, I did not find a proper insertion type.

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.