How To Copy DataRows Between DataTables by Using Visual Basic .NET
This article refers to the following Microsoft .NET Framework Class Library namespaces:
IN THIS TASK
How to Copy DataRows Between DataTablesBefore you use the ImportRow method, you must ensure that the target table has the identical structure as the source table. This sample uses the Clone method of DataTable class to copy the structure of the DataTable, including all DataTable schemas, relations, and constraints.
This sample uses the Products table that is included with the Microsoft SQL Server Northwind database. The first five rows are copied from the Products table to another table that is created in memory.
- Create a new Visual Basic .NET Console Application.
- If the Code window is not open already, right-click Module1.vb in the Solution Explorer window, and click View Code.
- Delete all of the code from the Code window.
- Copy the following code and paste it in the Code window:
Imports System.DataImports System.Data.SqlClientModule Module1 Sub Main() Dim ds As DataSet = New DataSet() Dim tblProducts As DataTable Dim tblProductsCopy As DataTable Dim tblProductsCount As Integer Dim tblProductsCopyCount As Integer Dim i As Integer 'Change the connection string to your server. Dim conn As SqlConnection = New SqlConnection("Server=ServerName;database=Northwind;UID=<User ID>;PWD=<PassWord>") 'Create the DataAdapter. Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from products", conn) 'Fill the DataSet with data. da.Fill(ds, "products") tblProducts = ds.Tables("products") tblProductsCount = tblProducts.Rows.Count 'Write the number of rows in Products table to the screen. Console.WriteLine("Table tblProducts has " & tblProductsCount.ToString & " Rows") 'Loop through the top five rows and write the first column to the screen. For i = 0 To 4 Console.WriteLine("Row(" & i.ToString & ") = " & tblProducts.Rows(i)(1)) Next 'The Clone method makes a copy of the table structure (Schema). tblProductsCopy = tblProducts.Clone 'Use ImportRow method to copy from Products table to its clone. For i = 0 To 4 tblProductsCopy.ImportRow(tblProducts.Rows(i)) Next tblProductsCopyCount = tblProductsCopy.Rows.Count 'Write blank line. Console.WriteLine() 'Write the number of rows in tblProductsCopy table to the screen. Console.WriteLine("Table tblProductsCopy has " & tblProductsCopyCount.ToString & " Rows") 'Loop through the top five rows and write the first column to the screen. For i = 0 To tblProductsCopyCount - 1 Console.WriteLine("Row(" & i.ToString & ") = " & tblProductsCopy.Rows(i)(1)) Next 'This line keeps the console open until you press ENTER. Console.ReadLine() End SubEnd Module
- Press the F5 key to build and run the project. Note that the program's output appears as follows in the Command window:
Table tblProducts has 77 RowsRow(0) = ChaiRow(1) = ChangRow(2) = Aniseed SyrupRow(3) = Chef Anton's Cajun SeasoningRow(4) = Chef Anton's Gumbo MixTable tblProductsCopy has 5 RowsRow(0) = ChaiRow(1) = ChangRow(2) = Aniseed SyrupRow(3) = Chef Anton's Cajun SeasoningRow(4) = Chef Anton's Gumbo Mix
- When you are finished, press ENTER to close the Command window.
Additional NotesYou can use the Copy method of a DataTable object to copy the entire DataTable:
Dim MyDataRow As DataRowDim DataTable1 As New DataTable()Dim DataTable2 As New DataTable()Dim DataView1 As New DataView()Dim DataSet1 as New DataSet()' Copy the entire DataTable.Dim dataTable2 = dataTable1.Copy()dataSet1.Tables.Add(dataTable2)
'Copy from the results of a Select method.For Each MydataRow In DataTable1.Select("Region = 'WA'") DataTable2.ImportRow(MyDataRow)Next MyDataRow'Copy from the results of a DataView.DataView1 = DataTable1.DefaultView()DataView1.RowFilter = "Region = 'WA'"For I = 0 To Dataview1.Count - 1 DataTable2.ImportRow(Dataview1.Item(I).Row)Next I
Article ID: 305346 - Last Review: 05/13/2007 05:00:21 - Revision: 2.5
- kbdatabase kbhowtomaster KB305346