Bulk loading data from one table to another database (VB) : SqlBulkCopy « ADO.net Database « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » ADO.net Database » SqlBulkCopy 
18. 53. 2. Bulk loading data from one table to another database (VB)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>

<script runat="server">
    Sub btnBulkCopy_Click(ByVal sender As Object, ByVal e As System.EventArgsHandles btnBulkCopy.Click

        Dim YourDatabaseConString As String
        Dim NorthWindConString As String
        Dim YourDatabaseCon As SqlConnection = New SqlConnection()
        Dim NorthwindCon As SqlConnection = New SqlConnection()

        Dim YourDatabaseCom As SqlCommand = New SqlCommand()
        Dim YourDatabaseReader As SqlDataReader

        YourDatabaseConString = ConfigurationManager.ConnectionStrings("DSN_YourDatabase").ConnectionString
        NorthWindConString = ConfigurationManager.ConnectionStrings("DSN_Northwind").ConnectionString

        YourDatabaseCon.ConnectionString = YourDatabaseConString
        YourDatabaseCom.Connection = YourDatabaseCon
        YourDatabaseCom.CommandText = " SELECT ID, First_Name, Last_Name, " & _
                                " 'YourDatabase' as Source FROM MailingList_Temp "
        YourDatabaseCom.CommandType = CommandType.Text

        YourDatabaseCom.Connection.Open()

        Dim NorthWindBulkOp As SqlBulkCopy
        NorthWindBulkOp = New SqlBulkCopy(NorthWindConString, _
                                SqlBulkCopyOptions.UseInternalTransaction)

        NorthWindBulkOp.DestinationTableName = "Employees"
        NorthWindBulkOp.ColumnMappings.Add("Id""EmployeeID")
        NorthWindBulkOp.ColumnMappings.Add("First_Name""FirstName")
        NorthWindBulkOp.ColumnMappings.Add("Last_Name""LastName")

        Dim JobTitleColMap As SqlBulkCopyColumnMapping
        JobTitleColMap = New SqlBulkCopyColumnMapping("Source""Title")

        NorthWindBulkOp.ColumnMappings.Add(JobTitleColMap)
        NorthWindBulkOp.BulkCopyTimeout = 500000000

        AddHandler NorthWindBulkOp.SqlRowsCopied, _
                    New SqlRowsCopiedEventHandler(AddressOf OnSqlRowsCopied)

        NorthWindBulkOp.NotifyAfter = 1000

        YourDatabaseReader = YourDatabaseCom.ExecuteReader()

        Try
            NorthWindBulkOp.WriteToServer(YourDatabaseReader)
        Catch ex As Exception
            ' Write error handling code here
            lblResult.Text = ex.Message
        Finally
            YourDatabaseReader.Close()
        End Try

    End Sub

    Private Sub OnSqlRowsCopied(ByVal sender As Object, _
            ByVal args As SqlRowsCopiedEventArgs)

        lblCounter.Text += args.RowsCopied.ToString() " rows are copied<Br>"
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Bulk Loading Large Volume Data</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnBulkCopy" Runat="server" Text="Start Bulk Copy" />&nbsp;
        <br />
        <br />
        <asp:Label ID="lblResult" Runat="server"></asp:Label>
        <br />
        <br />
        <asp:Label ID="lblCounter" Runat="server"></asp:Label>    
    </div>
    </form>
</body>
</html>

File: Web.config

<configuration>

  <connectionStrings>
        <add name="DSN_Northwind" 
             connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
             providerName="System.Data.SqlClient" />

        <add name="DSN_YourDatabase" 
             connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=YourDatabase;Integrated Security=True"
             providerName="System.Data.SqlClient" />    </connectionStrings>

</configuration>
18. 53. SqlBulkCopy
18. 53. 1. Bulk loading data from one table to another database (C#)
18. 53. 2. Bulk loading data from one table to another database (VB)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.