.NET
  Home arrow .NET arrow Page 4 - .NET Stored Procedures: Autogenerating INS...
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 Procedures: Autogenerating INSERT Statements for rows in Oracle Tables
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2005-12-20

    Table of Contents:
  • .NET Stored Procedures: Autogenerating INSERT Statements for rows in Oracle Tables
  • Developing Oracle based .NET CLR stored procedure using Visual Studio.NET
  • Understanding the .NET CLR Stored Procedure
  • Understanding the .NET CLR Stored Procedure - continued
  • Deploying and testing the Oracle based .NET CLR stored procedure using Visual Studio.NET

  • 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 Procedures: Autogenerating INSERT Statements for rows in Oracle Tables - Understanding the .NET CLR Stored Procedure - continued


    (Page 4 of 5 )

    In the previous section, we discussed some of the parts of the stored procedure.  In this section, we shall extend the same discussion further.

    Let us consider the following code fragment:

            Dim dr As DataRow
            Dim dc As DataColumn
            Dim dt As DataTable = ds.Tables("temp")

    Those are the declarations needed to hold each row (in “dr”), each column (in “dc”) available from the table (“dt”) extracted from the data set (“ds”).  Continuing, we have the following:

     Dim sw As StreamWriter = New StreamWriter(FilePath)
            For Each dr In dt.Rows
                Dim ins As String = ""
                ins = "INSERT INTO "
                ins &= TableName & " "
                ins &= "( " 'starting column names

    In the above code, we started to create the text file using “StreamWriter”.  We also started a loop to go through all the rows available in the data table.  Every row tries to generate a new INSERT statement (into “ins”).  We start framing the INSERT statement as above.  At this moment, no column names are specified.  We need to add the column names dynamically.  The following code does this:

                'adding column names
                For Each dc In dt.Columns
                    ins &= dc.ColumnName & ", "
                Next
                ins = ins.TrimEnd.TrimEnd(",")
                ins &= ") " 'finished column names
                ins &= " values "
                ins &= "( " 'starting column values

    Within the above code fragment, we are going through each column available within that row and adding the column name to the INSERT statement.  Once the adding is completed, we need to remove the last comma and close the columns section.  Next we open the “values” section and start adding the values using the following code:

                For Each dc In dt.Columns

                    If IsDBNull(dr(dc.ColumnName)) Then
                        ins &= "null, "
                    Else
                        Select Case dc.DataType.Name
                            Case "String", "DateTime"
                                ins &= "'" & dr(dc.ColumnName) & "', "
                            Case Else
                                ins &= dr(dc.ColumnName) & ", "
                        End Select
                    End If
                Next
                ins = ins.TrimEnd.TrimEnd(",")
                ins &= "); " 'finished column values
                sw.WriteLine(ins)
            Next
            sw.Close()

    In the above code, I am testing mainly for NULLS and data types.  I am generating the INSERT statement accordingly based on the data types.  Finally, we write the INSERT statement into the file using the “sw.Writeline” statement.  Finally, we close the file with the last statement in the above code fragment.

    More .NET Articles
    More By Jagadish Chaterjee


       · Hello guys, this is my second article on .NET CLR stored procedures in Oracle. Enjoy...
     

    .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 3 hosted by Hostway
    Stay green...Green IT