Click here to Skip to main content
15,867,972 members
Articles / Database Development / SQL Server
Article

Oracle to SQL Server VB.NET Utility application

Rate me:
Please Sign up or sign in to vote.
4.76/5 (24 votes)
7 Jun 20073 min read 103.9K   2.8K   67   10
A utility program that allows you to enter SQL to select data from Oracle, the program will then create a table in SQL Server and copy the data using SQLBulkCopy.

Introduction

This is a small utility application that I wrote to help retrieve data from an Oracle source database, then to create the structure dynamically in SQL Server and perform a Bulk Copy. I put a GUI on top of it so that you can use this to test multiple connections and queries.

This utility performs the following steps:

  1. Retrieves the data from Oracle through a OracleDataReader
  2. Converts the OracleDataReader to a DataTable
  3. Creates the SQL Server Tables based on the DataTable Structure
  4. Performs a SQLBulkCopy of the DataTable into the new SQL Server tables.

Screenshot - screen.jpg

Background

Why would you ever want to perform this? Let's face it, not all of your data requirements for a given application or report are going to reside in one place, especially in large organizations. I was looking for a quick way to perform a SQL query against Oracle and bring the data into SQL Server. This was finally the solution, and it does NOT require the Oracle client to be installed. Since this utility will be deployed to a server where it will be scheduled to run queries, I didn't want to have to install the Oracle client. So I used the Oracle Instant Client.

The code performs fairly well and I am fairly pleased with the little code it took to perform this. There are no loops through the DataSet or DataTable. Most conversion functions are done using built in procedures.

Prerequisites

Before doing anything, you will need to download the Oracle Instant Client. This is a zip file: instantclient-basic-win32-10.2.0.3-20061115.zip that contains several DLL and jar files that are required to connect to the Oracle database. YOU DO NOT NEED TO INSTALL THE ORACLE CLIENT. Simply place these files in the same folder as your .exe file. You will also need to create or copy a TNSNAMES.ORA file into the same directory.

Download the instant client from here:
http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.html

Using the code

ConnectandQuery() is the main routine. From here I call all the sub routines and other helper classes to achieve the objectives.

VB.NET
Private Sub ConnectAndQuery()
    Dim connectionString As String = GetConnectionString()
    Dim connection As OracleConnection = New OracleConnection
    Dim oConn As SqlConnection

    Try
    'CONNECT TO ORACLE AND EXECUTE THE QUERY
        connection.ConnectionString = connectionString
        connection.Open()
        Dim command As OracleCommand = connection.CreateCommand
        Dim sql As String = TextBox2.Text
        command.CommandText = sql
        Dim reader As OracleDataReader = command.ExecuteReader

        'CONVERT DATAREADER TO DATATABLE
        Dim dra As DataUtils.DataReaderAdapter = &_
        New DataUtils.DataReaderAdapter
        Dim dt As DataTable = New DataTable
        dra.FillFromReader(dt, reader)
        reader.Close()

        'BIND THE DATATABLE TO THE DATAGRIDVIEW
        DataGridView1.DataSource = dt

        'Create the SQL Server Table
        oConn = New SqlConnection(txtDestinationConnString.Text)
        oConn.Open()
        Dim sqlTableCreator As SQLCreateTable = New SQLCreateTable
        sqlTableCreator.Connection = oConn
        sqlTableCreator.DestinationTableName = txtDestTableName.Text
        sqlTableCreator.DropTableIfExists = CheckBox1.Checked
        sqlTableCreator.CreateFromDataTable(dt)

        'INSERT INTO SQL SERVER DATABASE
        CopyData(dt, oConn)
    Catch ex As Exception
        msgOut(ex.ToString)
    Finally
        CType(connection, IDisposable).Dispose()
        oConn.Close()
    End Try

End Sub

To convert the OracleDataReader to a DataTable, I overrode a protected Fill method that allows you to pass a DataTable and DataReader. For more information on this, please refer to the article that I used: http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=628

VB.NET
Imports System
Imports System.Data
Imports System.Data.Common
Namespace DataUtils
    Public Class DataReaderAdapter
        Inherits DbDataAdapter

        Public Function FillFromReader(ByVal dataTable As DataTable, &_
        ByVal dataReader As IDataReader) As Integer
            Return Me.Fill(dataTable, dataReader)
        End Function
... (rest of code not shown)

SQLBulkCopy in the System.Data.SQLClient namespace is an efficient class that allows you to copy large amounts of data from your .NET application to the SQL Server. This was introduced in the .NET Framework 2.0. Rather than inserting row by row which can take 13-15 seconds for a large recordset, using bulk copy, you can decrease it by 1/3 the time. This was important for me since some of the Oracle queries could return quite a lot of data. Please refer to the following article for more information on SQLBulkCopy:
http://www.sqlteam.com/item.asp?ItemID=26941

VB.NET
Private Sub CopyData(ByVal sourceTable As DataTable, &_
    ByVal destConnection As SqlConnection)
    Dim s As SqlBulkCopy = New SqlBulkCopy(destConnection)
    Try
        s.DestinationTableName = "OraTest3"
        s.NotifyAfter = 10000
        AddHandler s.SqlRowsCopied, AddressOf s_SqlRowsCopied
        s.WriteToServer(sourceTable)
        s.Close()
    Finally
        CType(s, IDisposable).Dispose()
    End Try
End Sub

Points of Interest

I enjoyed writing this program. It took me a while to figure out what the best way was to connect to Oracle. Also the corporate setup we have for connecting to Oracle databases uses SQLNet.ORA instead of TNSNames.ORA. I also had a lot of conflicts with running 2 versions of the Oracle Client. I finally read some articles that Oracle 10g does not play nicely with other Oracle Client versions such as 9i. In the end I decided to just remove all installations of the Oracle client and just use a TNSNames.ORA file. I hope someone is able to reuse this or some of the concepts at some point. Also, please let me know if you find ways to improve this code or a more preferred way to do the transform or bulk copy.

History

  • 6/7/07: Original Posting

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have been programming in .net since ASPX, just prior to the release of ASP.net. I have exposure to both web & client side development. In .NET I have permormed the role of developer, technical lead, and an architect. In 2003, I was the technical lead and architect on a project which received Computer World Magazine's Award for Best Practices in Mobile Computing for a .NET application.

In my current profession, I am a consultant now as a project manger for the past 3 years at a fortune 100 company. Most of PM work is business, meetings, and dealing with issues. However, I still enjoy programming in .net whenever I get the chance.

I am also extremely active in my current web development and consulting business: www.reassent.com. If you are interested in web development with DotNetNuke or .Net work, please feel free to visit the website or contact me directly.

Respectfully,
hoquet

Comments and Discussions

 
QuestionHelp me Pin
Member 1097722914-Aug-14 16:05
Member 1097722914-Aug-14 16:05 
Bugsmall bug fix Pin
Bruce Kunkle9-Jul-13 6:37
Bruce Kunkle9-Jul-13 6:37 
GeneralSuggested improvement Pin
David Catriel13-Sep-07 7:31
David Catriel13-Sep-07 7:31 
GeneralOracle to SQL Server vb.net utility application Pin
Srinivasan Seshadri25-Jul-07 18:18
Srinivasan Seshadri25-Jul-07 18:18 
GeneralCongratulations Pin
Alaa Jubran25-Jul-07 1:23
Alaa Jubran25-Jul-07 1:23 
QuestionWhy not use SQL DTS/SISS? Pin
Abi Bellamkonda23-Jul-07 16:01
Abi Bellamkonda23-Jul-07 16:01 
GeneralSQL 2000 Pin
valhoti@yahoo.com22-Jun-07 0:08
valhoti@yahoo.com22-Jun-07 0:08 
GeneralRe: SQL 2000 Pin
hoquet22-Jun-07 5:32
hoquet22-Jun-07 5:32 
Generalnice and easy. Pin
commandant_.16-Jun-07 23:21
commandant_.16-Jun-07 23:21 
GeneralExcellent work!!! Pin
dotnetangel7-Jun-07 8:41
professionaldotnetangel7-Jun-07 8:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.