- Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathsample2.cs
160 lines (142 loc) · 4.63 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
154
155
156
157
158
159
160
/* 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.
*
*****************************************************************************/
usingSystem;
usingSystem.Data;
usingSystem.Text;
usingOracle.ManagedDataAccess.Client;
usingOracle.ManagedDataAccess.Types;
namespaceSample2
{
/// <summary>
/// Sample 2: Demonstrates how a REF Cursor is obtained as an
/// OracleDataReader through the use of an OracleRefCursor object.
/// </summary>
classSample2
{
/// <summary>
/// The main entry point for the application.
/// </summary>
staticvoidMain(string[]args)
{
// Connect
stringconstr="User Id=scott;Password=<PASSWORD>;Data Source=oracle";
OracleConnectioncon=Connect(constr);
// Setup
Setup(con);
// Set the command
OracleCommandcmd=newOracleCommand(
"begin open :1 for select * from multimedia_tab where thekey = 1; end;",
con);
cmd.CommandType=CommandType.Text;
// Bind
OracleParameteroparam=cmd.Parameters.Add("refcur",OracleDbType.RefCursor);
oparam.Direction=ParameterDirection.Output;
try
{
// Execute command
cmd.ExecuteNonQuery();
// Obtain the OracleDataReader from the REF Cursor parameter
// oparam.Value returns an OracleRefCursor object.
// GetDataReader is a method of OracleRefCursor that returns an OracleDataReader object.
OracleDataReaderreader=(OracleDataReader)((OracleRefCursor)(oparam.Value)).GetDataReader();
// show the first row
reader.Read();
// Print out SCOTT.MULTIMEDIA_TAB THEKEY column
Console.WriteLine("THEKEY: {0}",reader.GetDecimal(0));
// Print out SCOTT.MULTIMEDIA_TAB STORY column
Console.WriteLine("STORY : {0}",reader.GetString(1));
}
catch(Exceptione)
{
Console.WriteLine("Error: {0}",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>
publicstaticOracleConnectionConnect(stringconnectStr)
{
OracleConnectioncon=newOracleConnection(connectStr);
try
{
con.Open();
}
catch(Exceptione)
{
Console.WriteLine("Error: {0}",e.Message);
}
returncon;
}
/// <summary>
/// Setup the necessary Tables & Test Data
/// </summary>
/// <param name="connectStr"></param>
publicstaticvoidSetup(OracleConnectioncon)
{
StringBuilderblr;
OracleCommandcmd=newOracleCommand("",con);
// Create multimedia table
blr=newStringBuilder();
blr.Append("DROP TABLE multimedia_tab");
cmd.CommandText=blr.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch
{
}
blr=newStringBuilder();
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(Exceptione)
{
Console.WriteLine("Error: {0}",e.Message);
}
blr=newStringBuilder();
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(Exceptione)
{
Console.WriteLine("Error: {0}",e.Message);
}
}
}
}