Skip to content

Commit 181a080

Browse files
authored
Update ArrayBind.cs (#395)
Updated sample to array bind 4 rows instead of 3 to avoid a square example as per customer suggestion. #368 Updated license link. Cleaned up comments and code artifacts for clarity.
1 parent d5ff3e0 commit 181a080

File tree

1 file changed

+96
-98
lines changed

1 file changed

+96
-98
lines changed

samples/array-bind/ArrayBind.cs

+96-98
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,114 @@
22
using System.Data;
33
using Oracle.ManagedDataAccess.Client;
44

5-
namespace ODPSample
5+
namespace ODPArrayBind
66
{
7-
/// <summary>
8-
/// Sample: Demonstrates array binding
9-
/// </summary>
10-
class ArrayBind
11-
{
12-
static void Main(string[] args)
7+
/// <summary>
8+
/// Sample: Demonstrates ODP.NET array binding
9+
/// </summary>
10+
class ArrayBind
1311
{
14-
// Connect
15-
// This sample code's DEPT table shares the same characteristics as SCOTT schema's DEPT table
16-
string connectStr = "User Id=<USER ID>;Password=<PASSWORD>;Data Source=<TNS ALIAS>";
17-
18-
// Setup the Tables for sample
19-
Setup(connectStr);
20-
21-
// Initialize array of data
22-
int[] myArrayDeptNo = new int[3]{1, 2, 3};
23-
String[] myArrayDeptName = {"Dev", "QA", "Facility"};
24-
String[] myArrayDeptLoc = {"New York", "Maryland", "Texas"};
25-
26-
OracleConnection connection = new OracleConnection(connectStr);
27-
OracleCommand command = new OracleCommand (
28-
"insert into dept values (:deptno, :deptname, :loc)", connection);
29-
30-
// Set the Array Size to 3. This applied to all the parameter in
31-
// associated with this command
32-
command.ArrayBindCount = 3;
33-
34-
// deptno parameter
35-
OracleParameter deptNoParam = new OracleParameter("deptno",OracleDbType.Int32);
36-
deptNoParam.Direction = ParameterDirection.Input;
37-
deptNoParam.Value = myArrayDeptNo;
38-
command.Parameters.Add(deptNoParam);
39-
40-
// deptname parameter
41-
OracleParameter deptNameParam = new OracleParameter("deptname", OracleDbType.Varchar2);
42-
deptNameParam.Direction = ParameterDirection.Input;
43-
deptNameParam.Value = myArrayDeptName;
44-
command.Parameters.Add(deptNameParam);
45-
46-
// loc parameter
47-
OracleParameter deptLocParam = new OracleParameter("loc", OracleDbType.Varchar2);
48-
deptLocParam.Direction = ParameterDirection.Input;
49-
deptLocParam.Value = myArrayDeptLoc;
50-
command.Parameters.Add(deptLocParam);
51-
52-
try
53-
{
54-
connection.Open();
55-
command.ExecuteNonQuery();
56-
Console.WriteLine("{0} Rows Inserted", command.ArrayBindCount);
57-
}
58-
catch (Exception e)
59-
{
60-
Console.WriteLine("Execution Failed:" + e.Message);
61-
}
62-
finally
63-
{
64-
// connection, command used server side resource, dispose them
65-
// asap to conserve resource
66-
connection.Close();
67-
command.Dispose();
68-
connection.Dispose();
69-
}
70-
}
12+
static void Main(string[] args)
13+
{
14+
// Connect
15+
// This sample code's DEPT table shares the same characteristics as the SCOTT schema's DEPT table.
16+
string connectStr = "User Id=<USER ID>;Password=<PASSWORD>;Data Source=<TNS ALIAS>";
7117

72-
public static void Setup(string connectStr)
73-
{
74-
int[] myArrayDeptNo = new int[3]{1, 2, 3};
75-
76-
OracleConnection conn = new OracleConnection(connectStr);
77-
OracleCommand cmd = new OracleCommand("delete dept where deptno = :1", conn);
78-
79-
// Bind with an array of 3 items
80-
cmd.ArrayBindCount = 3;
81-
82-
OracleParameter param1 = new OracleParameter();
83-
param1.OracleDbType = OracleDbType.Int32;
84-
param1.Value = myArrayDeptNo;
85-
86-
cmd.Parameters.Add(param1);
87-
88-
try
89-
{
90-
conn.Open();
91-
cmd.ExecuteNonQuery();
92-
}
93-
catch (Exception e)
94-
{
95-
Console.WriteLine("Setup Failed:{0}" ,e.Message);
96-
}
97-
finally
98-
{
99-
conn.Close();
100-
cmd.Dispose();
101-
}
18+
// Clear rows from past sample code executions
19+
Setup(connectStr);
20+
21+
// Initialize array of data
22+
int[] myArrayDeptNo = new int[4] { 1, 2, 3, 4 };
23+
String[] myArrayDeptName = { "Dev", "QA", "PM", "Integration" };
24+
String[] myArrayDeptLoc = { "California", "Arizona", "Texas", "Oregon" };
25+
26+
OracleConnection connection = new OracleConnection(connectStr);
27+
OracleCommand command = new OracleCommand(
28+
"insert into dept values (:deptno, :deptname, :loc)", connection);
29+
30+
// Set the array size to 4. This applies to all the command's associated parameters.
31+
command.ArrayBindCount = 4;
32+
33+
// Deptno parameter
34+
OracleParameter deptNoParam = new OracleParameter("deptno", OracleDbType.Int32);
35+
deptNoParam.Direction = ParameterDirection.Input;
36+
deptNoParam.Value = myArrayDeptNo;
37+
command.Parameters.Add(deptNoParam);
38+
39+
// Deptname parameter
40+
OracleParameter deptNameParam = new OracleParameter("deptname", OracleDbType.Varchar2);
41+
deptNameParam.Direction = ParameterDirection.Input;
42+
deptNameParam.Value = myArrayDeptName;
43+
command.Parameters.Add(deptNameParam);
44+
45+
// Loc parameter
46+
OracleParameter deptLocParam = new OracleParameter("loc", OracleDbType.Varchar2);
47+
deptLocParam.Direction = ParameterDirection.Input;
48+
deptLocParam.Value = myArrayDeptLoc;
49+
command.Parameters.Add(deptLocParam);
50+
51+
try
52+
{
53+
connection.Open();
54+
command.ExecuteNonQuery();
55+
Console.WriteLine("{0} rows inserted", command.ArrayBindCount);
56+
}
57+
catch (Exception e)
58+
{
59+
Console.WriteLine("Execution failed:" + e.Message);
60+
}
61+
finally
62+
{
63+
// Dispose connection and command used server side resource
64+
connection.Close();
65+
command.Dispose();
66+
connection.Dispose();
67+
}
68+
}
69+
70+
public static void Setup(string connectStr)
71+
{
72+
int[] myArrayDeptNo = new int[4] { 1, 2, 3, 4 };
73+
74+
OracleConnection conn = new OracleConnection(connectStr);
75+
OracleCommand cmd = new OracleCommand("delete dept where deptno = :1", conn);
76+
77+
// Bind with a 4 item array
78+
cmd.ArrayBindCount = 4;
79+
80+
OracleParameter param1 = new OracleParameter();
81+
param1.OracleDbType = OracleDbType.Int32;
82+
param1.Value = myArrayDeptNo;
83+
84+
cmd.Parameters.Add(param1);
85+
86+
try
87+
{
88+
conn.Open();
89+
cmd.ExecuteNonQuery();
90+
}
91+
catch (Exception e)
92+
{
93+
Console.WriteLine("Setup Failed:{0}", e.Message);
94+
}
95+
finally
96+
{
97+
conn.Close();
98+
cmd.Dispose();
99+
}
100+
}
102101
}
103-
}
104102
}
105103

106-
/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. */
107-
104+
/* Copyright (c) 2017, 2024 Oracle and/or its affiliates. All rights reserved. */
105+
108106
/******************************************************************************
109107
*
110108
* You may not use the identified files except in compliance with The MIT
111109
* License (the "License.")
112110
*
113111
* You may obtain a copy of the License at
114-
* https://github.com/oracle/Oracle.NET/blob/master/LICENSE
112+
* https://github.com/oracle/Oracle.NET/blob/master/LICENSE.txt
115113
*
116114
* Unless required by applicable law or agreed to in writing, software
117115
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

0 commit comments

Comments
 (0)