Bulk load rows in a DataTable that match a specified state : DataTable « Database ADO.net « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Database ADO.net » DataTable 
26.7.3.Bulk load rows in a DataTable that match a specified state
Imports System.Data
Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Using connection As SqlConnection = New SqlConnection("Data Source=(local);Integrated Security=true;Initial Catalog=AdventureWorks;")
            connection.Open()
            Dim commandRowCount As New SqlCommand("SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;", connection)
            Dim countStart As Long = System.Convert.ToInt32(commandRowCount.ExecuteScalar())

            Console.WriteLine("Starting row count = {0}", countStart)

            Dim newProducts As DataTable = New DataTable("NewProducts")
            Dim productID As DataColumn = New DataColumn()
            productID.DataType = System.Type.GetType("System.Int32")
            productID.ColumnName = "ProductID"
            productID.AutoIncrement = True
            newProducts.Columns.Add(productID)

            Dim productName As DataColumn = New DataColumn()
            productName.DataType = System.Type.GetType("System.String")
            productName.ColumnName = "Name"
            newProducts.Columns.Add(productName)

            Dim productNumber As DataColumn = New DataColumn()
            productNumber.DataType = System.Type.GetType("System.String")
            productNumber.ColumnName = "ProductNumber"
            newProducts.Columns.Add(productNumber)

            Dim keys(0As DataColumn
            keys(0= productID
            newProducts.PrimaryKey = keys

            Dim row As DataRow
            row = newProducts.NewRow()
            row("Name""A"
            row("ProductNumber""A1"
            newProducts.Rows.Add(row)

            row = newProducts.NewRow()
            row("Name""B"
            row("ProductNumber""B1"
            newProducts.Rows.Add(row)

            row = newProducts.NewRow()
            row("Name""C"
            row("ProductNumber""C1"
            newProducts.Rows.Add(row)
            newProducts.AcceptChanges()

            row = newProducts.Rows(0)
            row.BeginEdit()
            row("Name""AAA"
            row.EndEdit()

            Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connection)
                bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"
                Try
                    bulkCopy.WriteToServer(newProducts, DataRowState.Unchanged)
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
            End Using

            Dim countEnd As Long = System.Convert.ToInt32(commandRowCount.ExecuteScalar())
            Console.WriteLine("Ending row count = {0}", countEnd)
            Console.WriteLine("{0} rows were added.", countEnd - countStart)

        End Using
    End Sub
End Module
26.7.DataTable
26.7.1.Output data in DataTable to XML fileOutput data in DataTable to XML file
26.7.2.Filter DataTable by a logic conditionFilter DataTable by a logic condition
26.7.3.Bulk load rows in a DataTable that match a specified state
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.