-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathSample2.cs
153 lines (138 loc) · 4.33 KB
/
Sample2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. */
/******************************************************************************
*
* You may not use the identified files except in compliance with The MIT
* License (the "License.")
*
* You may obtain a copy of the License at
* https://github.com/oracle/Oracle.NET/blob/master/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*****************************************************************************/
using System;
using System.Data;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
namespace Sample2
{
/// <summary>
/// Sample 2: Demonstrates how an OracleClob object is obtained
/// as an output parameter of an anonymous PL/SQL block
/// </summary>
class Sample2
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
// Connect
string constr = "User Id=scott;Password=<PASSWORD>;Data Source=oracle";
OracleConnection con = Connect(constr);
// Setup
Setup(con);
// Set the command
OracleCommand cmd = new OracleCommand(
"begin select story into :1 from multimedia_tab where thekey = 1; end;");
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
// Bind a parameter
OracleParameter param = cmd.Parameters.Add("clobdata",
OracleDbType.Clob);
param.Direction = ParameterDirection.Output;
// Execute command
try
{
cmd.ExecuteNonQuery();
// Obtain LOB data as a .NET Type.
// cmd.Parameters[0].Value is an object of OracleClob.
// OracleClob.Value property retuns CLOB data as a string.
string lob_data = (string) ((OracleClob)(cmd.Parameters[0].Value)).Value;
// Print out the text
Console.WriteLine("Data is: " + lob_data);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// Dispose OracleCommand object
cmd.Dispose();
// Close and Dispose OracleConnection object
con.Close();
con.Dispose();
}
}
/// <summary>
/// Wrapper for Opening a new Connection
/// </summary>
/// <param name="connectStr"></param>
/// <returns></returns>
public static OracleConnection Connect(string connectStr)
{
OracleConnection con = new OracleConnection(connectStr);
try
{
con.Open();
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
return con;
}
/// <summary>
/// Setup the necessary Tables & Test Data
/// </summary>
/// <param name="connectStr"></param>
public static void Setup(OracleConnection con)
{
StringBuilder blr;
OracleCommand cmd = new OracleCommand("", con);
blr = new StringBuilder();
blr.Append("DROP TABLE multimedia_tab");
cmd.CommandText = blr.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch
{
}
blr = new StringBuilder();
blr.Append("CREATE TABLE multimedia_tab(thekey NUMBER(4) PRIMARY KEY,");
blr.Append("story CLOB, sound BLOB)");
cmd.CommandText = blr.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
blr = new StringBuilder();
blr.Append("INSERT INTO multimedia_tab values(");
blr.Append("1,");
blr.Append("'This is a long story. Once upon a time ...',");
blr.Append("'656667686970717273747576777879808182838485')");
cmd.CommandText = blr.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
}
}