Skip to content

Commit 9c2e033

Browse files
authored
Create oracle-bulk-copy.cs
1 parent abb5946 commit 9c2e033

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Oracle.ManagedDataAccess.Client;
2+
using System;
3+
4+
class Program
5+
{
6+
static void Main()
7+
{
8+
string connectionString = GetConnectionString();
9+
// Open a connection to the DB.
10+
using (OracleConnection sourceConnection = new OracleConnection(connectionString))
11+
{
12+
sourceConnection.Open();
13+
14+
// Perform an initial count on the source/destination table.
15+
OracleCommand commandRowCount = new OracleCommand("SELECT COUNT(*) FROM BLOGS", sourceConnection);
16+
long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
17+
Console.WriteLine("Starting row count = {0}", countStart);
18+
19+
// Get data from the source table as a OracleDataReader.
20+
OracleCommand commandSourceData = new OracleCommand("SELECT ID, URL FROM BLOGS", sourceConnection);
21+
OracleDataReader reader = commandSourceData.ExecuteReader();
22+
23+
// Open the destination connection. In the real world you would
24+
// not use OracleBulkCopy to move data from one table to the other
25+
// in the same database. This is for demonstration purposes only.
26+
using (OracleConnection destinationConnection = new OracleConnection(connectionString))
27+
{
28+
destinationConnection.Open();
29+
30+
// Set up the bulk copy object.
31+
// Note that the column positions in the source data reader match the column positions in
32+
// the destination table so there is no need to map columns.
33+
using (OracleBulkCopy bulkCopy = new OracleBulkCopy(destinationConnection))
34+
{
35+
bulkCopy.DestinationTableName = "BLOGS";
36+
37+
try
38+
{
39+
// Write from the source to the destination.
40+
bulkCopy.WriteToServer(reader);
41+
}
42+
catch (Exception ex)
43+
{
44+
Console.WriteLine(ex.Message);
45+
}
46+
finally
47+
{
48+
// Close the OracleDataReader. The OracleBulkCopy object is automatically
49+
// closed at the end of the using block.
50+
reader.Close();
51+
}
52+
}
53+
54+
// Perform a final count on the destination table to see how many rows were added.
55+
long countEnd = System.Convert.ToInt32(
56+
commandRowCount.ExecuteScalar());
57+
Console.WriteLine("Ending row count = {0}", countEnd);
58+
Console.WriteLine("{0} rows were added.", countEnd - countStart);
59+
Console.WriteLine("Press Enter to finish.");
60+
Console.ReadLine();
61+
}
62+
}
63+
}
64+
65+
private static string GetConnectionString()
66+
// Enter credentials below.
67+
{
68+
return "Data Source=<Data Source>; " +
69+
"User Id=<User Id>;" +
70+
"Password=<Password>;";
71+
}
72+
}
73+
74+
/* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. */
75+
/* Copyright (c) Microsoft */
76+
77+
/******************************************************************************
78+
* Licensed under the Apache License, Version 2.0 (the "License");
79+
* you may not use this file except in compliance with the License.
80+
* You may obtain a copy of the License at
81+
*
82+
* http://www.apache.org/licenses/LICENSE-2.0
83+
*
84+
* Unless required by applicable law or agreed to in writing, software
85+
* distributed under the License is distributed on an "AS IS" BASIS,
86+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
87+
* See the License for the specific language governing permissions and
88+
* limitations under the License.
89+
*
90+
*****************************************************************************/

0 commit comments

Comments
 (0)