Microsoft KB Archive/308909

= How To Copy DataRows Between DataTables by Using Visual C# .NET =

Article ID: 308909

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q308909



For a Microsoft Visual Basic .NET version of this article, see 305346.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * How to Copy DataRows Between DataTables
 * Additional Notes

REFERENCES



SUMMARY
This article demonstrates how to copy specific DataRow objects from one table to another by using the ImportRow method of the DataTable class.

back to the top

How to Copy DataRows Between DataTables
Before 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 C# Console Application project. Class1.cs is created by default. If the Code window is not open, right-click Class1.cs in Solution Explorer, and click View Code. Delete all of the code from the Code window.  Copy the following code, and paste it in the Code window: using System; using System.Data; using System.Data.SqlClient;

namespace ImportRowCS {   class Class1 {       static void Main(string[] args) {           //             // TODO: Add code to start application here. DataTable tblProducts = new DataTable; DataTable tblProductsCopy = new DataTable;

int tblProductsCount; int tblProductsCopyCount; int i;   // Change the connection string to your server. SqlConnection Conn = new SqlConnection(&quot;Server=(local);                                           database=Northwind;                                            UID=;PWD=&quot;); // Create the DataAdapter. SqlDataAdapter da = new SqlDataAdapter(&quot;Select * from products&quot;, Conn); // Fill the DataSet with data. DataSet ds = new DataSet; da.Fill(ds, &quot;products&quot;); tblProducts = ds.Tables[&quot;products&quot;]; tblProductsCount = tblProducts.Rows.Count;

// Write the number of rows in the Products table to the screen. Console.WriteLine(&quot;Table tblProducts has &quot; + tblProductsCount.ToString + &quot; Rows&quot;);

// Loop through the top five rows, and write the first column to the screen. for (i=0; i<=4;++i) {               Console.WriteLine(&quot;Row(&quot; + i.ToString + &quot;) = &quot; + tblProducts.Rows[i][1]); }

// Use Clone method to copy the table structure (Schema). tblProductsCopy = tblProducts.Clone;

// Use the ImportRow method to copy from Products table to its clone. for (i=0; i<=4;++i) {           tblProductsCopy.ImportRow(tblProducts.Rows[i]); }       tblProductsCopyCount = tblProductsCopy.Rows.Count; // Write blank line. Console.WriteLine; // Write the number of rows in tblProductsCopy table to the screen. Console.WriteLine(&quot;Table tblProductsCopy has &quot; +                      tblProductsCopyCount.ToString + &quot; Rows&quot;);

// Loop through the top five rows, and write the first column to the screen. for(i = 0; i<=tblProductsCopyCount - 1;++i) {               Console.WriteLine(&quot;Row(&quot; + i.ToString + &quot;) = &quot; + tblProductsCopy.Rows[i][1]); }

// This line keeps the console open until you press ENTER. Console.ReadLine;

}   } }                     Modify the connection string as appropriate for your environment. Press the F5 key to build and run the project. Note that the program's output appears as follows in the Command window:

<pre class="fixed_text">Table tblProducts has 77 Rows Row(0) = Chai Row(1) = Chang Row(2) = Aniseed Syrup Row(3) = Chef Anton's Cajun Seasoning Row(4) = Chef Anton's Gumbo Mix

Table tblProductsCopy has 5 Rows Row(0) = Chai Row(1) = Chang Row(2) = Aniseed Syrup Row(3) = Chef Anton's Cajun Seasoning Row(4) = Chef Anton's Gumbo Mix

</li> When you are finished, press ENTER to close the Command window.</li></ol>

back to the top

Additional Notes
You can use the Copy method of a DataTable object to copy the entire DataTable: DataTable DataTable1 = new DataTable; DataTable DataTable2 = new DataTable; DataView DataView1 = new DataView; DataSet DataSet1 = new DataSet;

// Copy the entire DataTable. DataTable2 = DataTable1.Copy; DataSet1.Tables.Add(DataTable2); You can also copy DataRow objects from the results of a filtered DataView class or from the results of a Select method. For example: // Copy from the results of a Select method. foreach (DataRow MyDataRow in DataTable1.Select(&quot;Region = 'WA'&quot;)) {   DataTable2.ImportRow(MyDataRow); } Console.WriteLine(DataTable2.Rows.Count); Console.ReadLine;

// Copy from the results of a DataView. DataView1 = DataTable1.DefaultView; DataView1.RowFilter = &quot;Region = 'WA'&quot;; for (int i = 0; i <= DataView1.Count - 1; ++i) {   DataTable2.ImportRow(DataView1[I].Row); } Console.WriteLine(DataTable2.Rows.Count); Console.ReadLine; back to the top

<div class="references_section">