.NET
  Home arrow .NET arrow Page 3 - .NET Stored Proceedures: Reading Excel Fil...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

.NET Stored Proceedures: Reading Excel Files or Transferring SQL Server Tables into Oracle Tables
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2006-01-03

    Table of Contents:
  • .NET Stored Proceedures: Reading Excel Files or Transferring SQL Server Tables into Oracle Tables
  • Developing an Oracle based .NET CLR stored procedure using Visual Studio.NET
  • Understanding the .NET CLR Stored Procedure (working with Excel)
  • Deploying and testing Oracle based .NET CLR stored procedure using Visual Studio.NET
  • Understanding the .NET CLR Stored Procedure (working with SQL Server 2000 table)

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    .NET Stored Proceedures: Reading Excel Files or Transferring SQL Server Tables into Oracle Tables - Understanding the .NET CLR Stored Procedure (working with Excel)


    (Page 3 of 5 )

    Before deploying the .NET stored procedure, let us go through some of the details of the previous program.  You must have observed, “imports System.data.OLEDB” at the top.  This is primarily required to deal with any “data source.”  The “data source” could be any database, file system, Mail system, Word, Excel, and so on.

    Let us go through the program part by part:

    Dim xlconn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\book1.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""")
            Dim xlda As New OleDbDataAdapter("select * from [sheet1$]", xlconn)
            Dim xldt As New DataTable
            xlda.Fill(xldt)
            xlda.Dispose()
            xlconn.Close()

    To work with Excel files, the first need is to connect to those files.  I created a connection object “xlconn” to deal with an Excel file.  I also created an object  “OledbDataAdapter” (xlda) to file the information available in the Excel file into a data table (xldt).   You must have observed the SELECT statement I wrote.  The table name is nothing but the Excel Sheet name.  Within the connection string, you should also observe the name of the Excel file I added.

    From the above part, we managed to fetch the information from an Excel file and fill in a data table.  As the reading of the Excel file is complete, the next issue is to transfer those rows into the Oracle table.  Let us continue with the second part.

    Dim conn As New OracleConnection("context connection=true")
            Dim da As New OracleDataAdapter("select * from scott.emp", conn)
            Dim cb As OracleCommandBuilder = New OracleCommandBuilder(da)
            Dim ds As New DataSet
            da.Fill(ds, "emp")

    Within the above code fragment, I am connecting to the Oracle database and creating an OracleDataAdapter object (da) to deal directly with the database table.  I am also creating an “OracleCommandBuilder” object to auto generate all the SQL commands (for INSERT, UPDATE, DELETE) internally for the OracleDataAdapter (da).  Finally, I fetch the “scott.emp” table into a “dataset” (ds) with a named data table (called “emp”).

    Now we need to loop through all the rows fetched from Excel and transfer them into the data table of the data set (ds).  Let us go through the following code fragment:

            For Each xlrow As DataRow In xldt.Rows
                Dim dr As DataRow = ds.Tables("emp").NewRow
                For Each col As DataColumn In ds.Tables("emp").Columns
                    dr(col.ColumnName) = xlrow(col.ColumnName)
                Next
                ds.Tables("emp").Rows.Add(dr)
            Next
            da.Update(ds, "emp")
            da.Dispose()
            conn.Close()

    The above code fragment contains two nested loops.  One works with the rows and the other (inner loop) works with columns.  For every iteration of the outer loop, I create a new row (for dataset “ds”) and populate (or transfer) the values from the data table “xldt” using the inner loop.

    The “da.Update()” automatically inserts all the copied rows from Excel into the Oracle table.  And finally we close and release all of the resources of the database connection.

    More .NET Articles
    More By Jagadish Chaterjee


       · Now I contributed .NET stored procedure to read and transfer Excel files and SQL...
       · i've used your third page information to insert records from excel into oracle db,...
       · Even though, I could not get a proper solution for your problem, I can provide you...
       · Sorry for troubling...I think i found the rootcause - my memory control...
       · Hai,I am very happy that you could find the root of your problem. In fact, your...
       · Hi,I have used your code example to export data from an Excel spreadsheet and...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT