C# 连接 Oracle,读取Blob字段数据,存到文件中去,包蕴pdf等等

Oracle,代码如下,记得引入Oracle的dll

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.Data.OracleClient;
10 using System.Data.SqlClient;
11 using System.IO;
12 
13 namespace WindowsFormsApplication3
14 {
15     public partial class Form1 : Form
16     {
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void simpleButton1_Click(object sender, EventArgs e)
23         {
24             var mServiceName = "20.";
25             var mUserName = "k_";
26             var  mPsw = "z!";
27             string connstring = string.Format("Data Source={0};User Id={1};Password={2}", mServiceName, mUserName, mPsw);
28             OracleConnection conn = new OracleConnection(connstring);
29             var a = "SELECT DOC_NT FROM dd WHERE  DOT_ID='6ff157b3-a482-4731-935d-782af8ca60cd'";
30             OracleCommand cmd = new OracleCommand(a, conn);
31             try
32             {
33                 conn.Open();
34                 OracleDataReader sdr = cmd.ExecuteReader();
35                 sdr.Read();
36                 var blob = new Byte[(sdr.GetBytes(0, 0, null, 0, int.MaxValue))];
37                 sdr.GetBytes(0, 0, blob, 0, blob.Length);
38                 sdr.Close();
39                 conn.Close();
40                 var  fs = new FileStream("c:\\abd.pdf", FileMode.Create, FileAccess.Write);
41                 fs.Write(blob, 0, blob.Length);
42                 fs.Close();
43               
44             }
45             catch (System.Exception ex)
46             {
47                 conn = null;
48                 throw ex;
49             }
50             finally
51             {
52                 if (conn != null && conn.State == System.Data.ConnectionState.Open)
53                     conn.Close();
54             }
55        
56         }
57     }
58 }

 

相关文章