<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FDatabase_ADO.net%2FDataTable</id>
		<title>Csharp/C Sharp/Database ADO.net/DataTable - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FDatabase_ADO.net%2FDataTable"/>
		<link rel="alternate" type="text/html" href="http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Database_ADO.net/DataTable&amp;action=history"/>
		<updated>2026-04-13T20:28:05Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Database_ADO.net/DataTable&amp;diff=941&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Database_ADO.net/DataTable&amp;diff=941&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:19Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Database_ADO.net/DataTable&amp;diff=942&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Database_ADO.net/DataTable&amp;diff=942&amp;oldid=prev"/>
				<updated>2010-05-26T11:42:27Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Filter sort based on DataTableCollection==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
   class FilterSort&lt;br /&gt;
   {&lt;br /&gt;
      static void Main(string[] args)&lt;br /&gt;
      {&lt;br /&gt;
         string connString = &amp;quot;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI&amp;quot;;&lt;br /&gt;
         string sql1 = @&amp;quot;select * from employee;&amp;quot;;&lt;br /&gt;
         string sql2 = @&amp;quot;select firstname, lastname from employee&amp;quot;;&lt;br /&gt;
         string sql = sql1 + sql2;&lt;br /&gt;
         SqlConnection conn = new SqlConnection(connString);&lt;br /&gt;
         try {&lt;br /&gt;
            SqlDataAdapter da = new SqlDataAdapter();&lt;br /&gt;
            da.SelectCommand = new SqlCommand(sql, conn);&lt;br /&gt;
            DataSet ds = new DataSet();&lt;br /&gt;
            da.Fill(ds, &amp;quot;employee&amp;quot;);&lt;br /&gt;
            DataTableCollection dtc = ds.Tables;&lt;br /&gt;
            Console.WriteLine(&amp;quot;Results from Customers table:&amp;quot;);&lt;br /&gt;
            Console.WriteLine(&amp;quot;FirstName&amp;quot;.PadRight(20) + &amp;quot;LastName&amp;quot;.PadLeft(23) + &amp;quot;\n&amp;quot;);&lt;br /&gt;
            string fl = &amp;quot;FirstName = &amp;quot;Z&amp;quot;&amp;quot;;&lt;br /&gt;
            string srt = &amp;quot;LastName asc&amp;quot;;&lt;br /&gt;
            foreach (DataRow row in dtc[&amp;quot;Employee&amp;quot;].Select(fl, srt))&lt;br /&gt;
            {&lt;br /&gt;
               Console.WriteLine(&lt;br /&gt;
                  &amp;quot;{0}\t{1}&amp;quot;, &lt;br /&gt;
                  row[&amp;quot;FirstName&amp;quot;].ToString().PadRight(25),&lt;br /&gt;
                  row[&amp;quot;LastName&amp;quot;]);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;Results from Products table:&amp;quot;);&lt;br /&gt;
            Console.WriteLine(&amp;quot;FirstName&amp;quot;.PadRight(20) + &amp;quot;LastName&amp;quot;.PadLeft(21) + &amp;quot;\n&amp;quot;);&lt;br /&gt;
            foreach (DataRow row in dtc[1].Rows){&lt;br /&gt;
               Console.WriteLine(&amp;quot;{0}\t{1}&amp;quot;, row[&amp;quot;FirstName&amp;quot;].ToString().PadRight(25),&lt;br /&gt;
                  row[&amp;quot;LastName&amp;quot;]);&lt;br /&gt;
            }&lt;br /&gt;
         } catch(Exception e) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Error: &amp;quot; + e);&lt;br /&gt;
         } finally {&lt;br /&gt;
            conn.Close();&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==illustrates how to specify and use a   relationship between two DataTable objects==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Mastering Visual C# .NET&lt;br /&gt;
by Jason Price, Mike Gunderloy&lt;br /&gt;
Publisher: Sybex;&lt;br /&gt;
ISBN: 0782129110&lt;br /&gt;
*/&lt;br /&gt;
/*&lt;br /&gt;
  Example23_6.cs illustrates how to specify and use a&lt;br /&gt;
  relationship between two DataTable objects&lt;br /&gt;
*/&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
public class Example23_6&lt;br /&gt;
{&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    // formulate a string containing the details of the&lt;br /&gt;
    // database connection&lt;br /&gt;
    string connectionString =&lt;br /&gt;
      &amp;quot;server=localhost;database=Northwind;uid=sa;pwd=sa&amp;quot;;&lt;br /&gt;
    // create a SqlConnection object to connect to the&lt;br /&gt;
    // database, passing the connection string to the constructor&lt;br /&gt;
    SqlConnection mySqlConnection =&lt;br /&gt;
      new SqlConnection(connectionString);&lt;br /&gt;
    // formulate a string containing a SELECT statement to&lt;br /&gt;
    // retrieve a row from the Customers table&lt;br /&gt;
    string selectString =&lt;br /&gt;
      &amp;quot;SELECT CustomerID, CompanyName &amp;quot; +&lt;br /&gt;
      &amp;quot;FROM Customers &amp;quot; +&lt;br /&gt;
      &amp;quot;WHERE CustomerID = &amp;quot;ALFKI&amp;quot;&amp;quot;;&lt;br /&gt;
    // create a SqlCommand object to hold the SELECT statement&lt;br /&gt;
    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();&lt;br /&gt;
    // set the CommandText property of the SqlCommand object to&lt;br /&gt;
    // the SELECT string&lt;br /&gt;
    mySqlCommand.rumandText = selectString;&lt;br /&gt;
    // create a SqlDataAdapter object&lt;br /&gt;
    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();&lt;br /&gt;
    // set the SelectCommand property of the SqlAdapter object&lt;br /&gt;
    // to the SqlCommand object&lt;br /&gt;
    mySqlDataAdapter.SelectCommand = mySqlCommand;&lt;br /&gt;
    // create a DataSet object to store the results of&lt;br /&gt;
    // the SELECT statement&lt;br /&gt;
    DataSet myDataSet = new DataSet();&lt;br /&gt;
    // open the database connection using the&lt;br /&gt;
    // Open() method of the SqlConnection object&lt;br /&gt;
    mySqlConnection.Open();&lt;br /&gt;
    // use the Fill() method of the SqlDataAdapter object to&lt;br /&gt;
    // retrieve the rows from the database, storing the rows&lt;br /&gt;
    // in a DataTable named &amp;quot;Customers&amp;quot;&lt;br /&gt;
    mySqlDataAdapter.Fill(myDataSet, &amp;quot;Customers&amp;quot;);&lt;br /&gt;
    // formulate a string containing a SELECT statement to&lt;br /&gt;
    // retrieve the rows from the Orders table where the CustomerID&lt;br /&gt;
    // column is equal to ALFKI&lt;br /&gt;
    selectString =&lt;br /&gt;
      &amp;quot;SELECT OrderID, CustomerID &amp;quot; +&lt;br /&gt;
      &amp;quot;FROM Orders &amp;quot; +&lt;br /&gt;
      &amp;quot;WHERE CustomerID = &amp;quot;ALFKI&amp;quot;&amp;quot;;&lt;br /&gt;
    // set the CommandText property of the SqlCommand object to&lt;br /&gt;
    // the SELECT string&lt;br /&gt;
    mySqlCommand.rumandText = selectString;&lt;br /&gt;
    // use the Fill() method of the SqlDataAdapter object to&lt;br /&gt;
    // retrieve the rows from the database, storing the rows&lt;br /&gt;
    // in a DataTable named &amp;quot;Orders&amp;quot;&lt;br /&gt;
    mySqlDataAdapter.Fill(myDataSet, &amp;quot;Orders&amp;quot;);&lt;br /&gt;
    // use the Add() method through the Relations property&lt;br /&gt;
    // to define a relationship between the Customers and&lt;br /&gt;
    // Orders DataTable objects&lt;br /&gt;
    myDataSet.Relations.Add(&lt;br /&gt;
      &amp;quot;Orders&amp;quot;,&lt;br /&gt;
      myDataSet.Tables[&amp;quot;Customers&amp;quot;].Columns[&amp;quot;CustomerID&amp;quot;],&lt;br /&gt;
      myDataSet.Tables[&amp;quot;Orders&amp;quot;].Columns[&amp;quot;CustomerID&amp;quot;]&lt;br /&gt;
    );&lt;br /&gt;
    // display the rows in the Customers and Orders DataTable objects,&lt;br /&gt;
    // using the GetChildRows() method to get the orders for the&lt;br /&gt;
    // customer&lt;br /&gt;
    DataTable customers = myDataSet.Tables[&amp;quot;Customers&amp;quot;];&lt;br /&gt;
    foreach (DataRow customer in customers.Rows)&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;CustomerID = &amp;quot; + customer[&amp;quot;CustomerID&amp;quot;]);&lt;br /&gt;
      Console.WriteLine(&amp;quot;CompanyName = &amp;quot; + customer[&amp;quot;CompanyName&amp;quot;]);&lt;br /&gt;
      DataRow[] orders = customer.GetChildRows(&amp;quot;Orders&amp;quot;);&lt;br /&gt;
      Console.WriteLine(&amp;quot;This customer placed the following orders:&amp;quot;);&lt;br /&gt;
      foreach (DataRow order in orders)&lt;br /&gt;
      {&lt;br /&gt;
        Console.WriteLine(&amp;quot;  OrderID = &amp;quot; + order[&amp;quot;OrderID&amp;quot;]);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    // close the database connection using the Close() method&lt;br /&gt;
    // of the SqlConnection object&lt;br /&gt;
    mySqlConnection.Close();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==illustrates the use of adding, modifying, and deleting   a row in a DataTable object and synchronizing those changes with the==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Mastering Visual C# .NET&lt;br /&gt;
by Jason Price, Mike Gunderloy&lt;br /&gt;
Publisher: Sybex;&lt;br /&gt;
ISBN: 0782129110&lt;br /&gt;
*/&lt;br /&gt;
/*&lt;br /&gt;
  Example23_3.cs illustrates the use of adding, modifying, and deleting&lt;br /&gt;
  a row in a DataTable object and synchronizing those changes with the&lt;br /&gt;
  database&lt;br /&gt;
*/&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
public class Example23_3&lt;br /&gt;
{&lt;br /&gt;
  public static void DisplayDataTable(DataTable myDataTable)&lt;br /&gt;
  {&lt;br /&gt;
    // display the columns for each row in the DataTable,&lt;br /&gt;
    // using a DataRow object to access each row in the DataTable&lt;br /&gt;
    foreach (DataRow myDataRow in myDataTable.Rows)&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;CustomerID = &amp;quot; + myDataRow[&amp;quot;CustomerID&amp;quot;]);&lt;br /&gt;
      Console.WriteLine(&amp;quot;CompanyName = &amp;quot; + myDataRow[&amp;quot;CompanyName&amp;quot;]);&lt;br /&gt;
      Console.WriteLine(&amp;quot;ContactName = &amp;quot; + myDataRow[&amp;quot;ContactName&amp;quot;]);&lt;br /&gt;
      Console.WriteLine(&amp;quot;Address = &amp;quot; + myDataRow[&amp;quot;Address&amp;quot;]);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void AddRow(&lt;br /&gt;
    DataTable myDataTable&lt;br /&gt;
  )&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;\nAdding a new row with CustomerID of &amp;quot;T1COM&amp;quot;&amp;quot;);&lt;br /&gt;
    // step 1: use the NewRow() method of the DataRow object to create&lt;br /&gt;
    // a new row in the DataTable&lt;br /&gt;
    DataRow myNewDataRow = myDataTable.NewRow();&lt;br /&gt;
    // step 2: set the values for the columns of the new row&lt;br /&gt;
    myNewDataRow[&amp;quot;CustomerID&amp;quot;] = &amp;quot;T1COM&amp;quot;;&lt;br /&gt;
    myNewDataRow[&amp;quot;CompanyName&amp;quot;] = &amp;quot;T1 Company&amp;quot;;&lt;br /&gt;
    myNewDataRow[&amp;quot;ContactName&amp;quot;] = &amp;quot;Jason Price&amp;quot;;&lt;br /&gt;
    myNewDataRow[&amp;quot;Address&amp;quot;] = &amp;quot;1 Main Street&amp;quot;;&lt;br /&gt;
    // step 3: use the Add() method through the Rows property to add&lt;br /&gt;
    // the new DataRow to the DataTable&lt;br /&gt;
    myDataTable.Rows.Add(myNewDataRow);&lt;br /&gt;
    // step 4: use the AcceptChanges() method of the DataTable to commit&lt;br /&gt;
    // the changes&lt;br /&gt;
    myDataTable.AcceptChanges();&lt;br /&gt;
  }&lt;br /&gt;
  public static void ModifyRow(&lt;br /&gt;
    DataTable myDataTable&lt;br /&gt;
  )&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;\nModifying the new row&amp;quot;);&lt;br /&gt;
    // step 1: set the PrimaryKey property for the DataTable object&lt;br /&gt;
    DataColumn[] myPrimaryKey = new DataColumn[1];&lt;br /&gt;
    myPrimaryKey[0] = myDataTable.Columns[&amp;quot;CustomerID&amp;quot;];&lt;br /&gt;
    myDataTable.PrimaryKey = myPrimaryKey;&lt;br /&gt;
    // step 2: use the Find() method to locate the DataRow&lt;br /&gt;
    // in the DataTable using the primary key value&lt;br /&gt;
    DataRow myEditDataRow = myDataTable.Rows.Find(&amp;quot;T1COM&amp;quot;);&lt;br /&gt;
    // step 3: change the column values&lt;br /&gt;
    myEditDataRow[&amp;quot;CompanyName&amp;quot;] = &amp;quot;Widgets Inc.&amp;quot;;&lt;br /&gt;
    myEditDataRow[&amp;quot;ContactName&amp;quot;] = &amp;quot;John Smith&amp;quot;;&lt;br /&gt;
    myEditDataRow[&amp;quot;Address&amp;quot;] = &amp;quot;1 Any Street&amp;quot;;&lt;br /&gt;
    // step 4: use the AcceptChanges() method of the DataTable to commit&lt;br /&gt;
    // the changes&lt;br /&gt;
    myDataTable.AcceptChanges();&lt;br /&gt;
    Console.WriteLine(&amp;quot;myEditDataRow.RowState = &amp;quot; + myEditDataRow.RowState);&lt;br /&gt;
  }&lt;br /&gt;
  public static void RemoveRow(&lt;br /&gt;
    DataTable myDataTable&lt;br /&gt;
  )&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;\nRemoving the new row&amp;quot;);&lt;br /&gt;
    // step 1: set the PrimaryKey property for the DataTable object&lt;br /&gt;
    DataColumn[] myPrimaryKey = new DataColumn[1];&lt;br /&gt;
    myPrimaryKey[0] = myDataTable.Columns[&amp;quot;CustomerID&amp;quot;];&lt;br /&gt;
    myDataTable.PrimaryKey = myPrimaryKey;&lt;br /&gt;
    // step 2: use the Find() method to locate the DataRow&lt;br /&gt;
    DataRow myRemoveDataRow = myDataTable.Rows.Find(&amp;quot;T1COM&amp;quot;);&lt;br /&gt;
    // step 3: use the Delete() method to remove the DataRow&lt;br /&gt;
    myRemoveDataRow.Delete();&lt;br /&gt;
    // step 4: use the AcceptChanges() method of the DataTable to commit&lt;br /&gt;
    // the changes&lt;br /&gt;
    myDataTable.AcceptChanges();&lt;br /&gt;
  }&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    // formulate a string containing the details of the&lt;br /&gt;
    // database connection&lt;br /&gt;
    string connectionString =&lt;br /&gt;
      &amp;quot;server=localhost;database=Northwind;uid=sa;pwd=sa&amp;quot;;&lt;br /&gt;
    // create a SqlConnection object to connect to the&lt;br /&gt;
    // database, passing the connection string to the constructor&lt;br /&gt;
    SqlConnection mySqlConnection =&lt;br /&gt;
      new SqlConnection(connectionString);&lt;br /&gt;
    // formulate a SELECT statement to retrieve the&lt;br /&gt;
    // CustomerID, CompanyName, ContactName, and Address&lt;br /&gt;
    // columns for the first row from the Customers table&lt;br /&gt;
    string selectString =&lt;br /&gt;
      &amp;quot;SELECT CustomerID, CompanyName, ContactName, Address &amp;quot; +&lt;br /&gt;
      &amp;quot;FROM Customers &amp;quot; +&lt;br /&gt;
      &amp;quot;WHERE CustomerID = &amp;quot;ALFKI&amp;quot;&amp;quot;;&lt;br /&gt;
    // create a SqlCommand object to hold the SELECT statement&lt;br /&gt;
    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();&lt;br /&gt;
    // set the CommandText property of the SqlCommand object to&lt;br /&gt;
    // the SELECT string&lt;br /&gt;
    mySqlCommand.rumandText = selectString;&lt;br /&gt;
    // create a SqlDataAdapter object&lt;br /&gt;
    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();&lt;br /&gt;
    // set the SelectCommand property of the SqlAdapter object&lt;br /&gt;
    // to the SqlCommand object&lt;br /&gt;
    mySqlDataAdapter.SelectCommand = mySqlCommand;&lt;br /&gt;
    // create a DataSet object to store the results of&lt;br /&gt;
    // the SELECT statement&lt;br /&gt;
    DataSet myDataSet = new DataSet();&lt;br /&gt;
    // open the database connection using the&lt;br /&gt;
    // Open() method of the SqlConnection object&lt;br /&gt;
    mySqlConnection.Open();&lt;br /&gt;
    // use the Fill() method of the SqlDataAdapter object to&lt;br /&gt;
    // retrieve the rows from the table, storing the rows locally&lt;br /&gt;
    // in a DataTable of the DataSet object&lt;br /&gt;
    Console.WriteLine(&amp;quot;Retrieving a row from the Customers table&amp;quot;);&lt;br /&gt;
    mySqlDataAdapter.Fill(myDataSet, &amp;quot;Customers&amp;quot;);&lt;br /&gt;
    // get the DataTable object from the DataSet object&lt;br /&gt;
    DataTable myDataTable = myDataSet.Tables[&amp;quot;Customers&amp;quot;];&lt;br /&gt;
    // display the rows in the DataTable object&lt;br /&gt;
    DisplayDataTable(myDataTable);&lt;br /&gt;
    // add a new row&lt;br /&gt;
    AddRow(myDataTable);&lt;br /&gt;
    DisplayDataTable(myDataTable);&lt;br /&gt;
    // modify a row&lt;br /&gt;
    ModifyRow(myDataTable);&lt;br /&gt;
    DisplayDataTable(myDataTable);&lt;br /&gt;
    // remove a row&lt;br /&gt;
    RemoveRow(myDataTable);&lt;br /&gt;
    DisplayDataTable(myDataTable);&lt;br /&gt;
    // use the Fill() method of the SqlDataAdapter object&lt;br /&gt;
    // to synchronize the changes with the database&lt;br /&gt;
    mySqlDataAdapter.Fill(myDataSet, &amp;quot;Customers&amp;quot;);&lt;br /&gt;
    // close the database connection using the Close() method&lt;br /&gt;
    // of the SqlConnection object&lt;br /&gt;
    mySqlConnection.Close();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Modify DataTable: insert data to database table==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
   class ModifyDataTable&lt;br /&gt;
   {&lt;br /&gt;
      static void Main()&lt;br /&gt;
      {&lt;br /&gt;
         string connString = &amp;quot;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI&amp;quot;;&lt;br /&gt;
         string sql = @&amp;quot;select * from employee&amp;quot;;&lt;br /&gt;
         SqlConnection conn = new SqlConnection(connString);&lt;br /&gt;
         try {&lt;br /&gt;
            SqlDataAdapter da = new SqlDataAdapter();&lt;br /&gt;
            da.SelectCommand = new SqlCommand(sql, conn);&lt;br /&gt;
            DataSet ds = new DataSet();   &lt;br /&gt;
            da.Fill(ds, &amp;quot;employee&amp;quot;);&lt;br /&gt;
            DataTable dt = ds.Tables[&amp;quot;employee&amp;quot;];&lt;br /&gt;
            dt.Columns[&amp;quot;firstname&amp;quot;].AllowDBNull = true;&lt;br /&gt;
            dt.Rows[0][&amp;quot;FirstName&amp;quot;] = &amp;quot;Joe&amp;quot;;&lt;br /&gt;
            DataRow newRow = dt.NewRow();&lt;br /&gt;
            newRow[&amp;quot;firstname&amp;quot;] = &amp;quot;Roy&amp;quot;;&lt;br /&gt;
            newRow[&amp;quot;lastname&amp;quot;] = &amp;quot;Beatty&amp;quot;;&lt;br /&gt;
            dt.Rows.Add(newRow);&lt;br /&gt;
            foreach (DataRow row in dt.Rows)&lt;br /&gt;
            {&lt;br /&gt;
               Console.WriteLine(&amp;quot;{0} {1}&amp;quot;,&lt;br /&gt;
                  row[&amp;quot;firstname&amp;quot;].ToString().PadRight(15),&lt;br /&gt;
                  row[&amp;quot;lastname&amp;quot;].ToString().PadLeft(25));&lt;br /&gt;
            }&lt;br /&gt;
            &lt;br /&gt;
         } catch(Exception e) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Error: &amp;quot; + e);&lt;br /&gt;
         } finally {&lt;br /&gt;
            conn.Close();&lt;br /&gt;
         }&lt;br /&gt;
      }  &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Print DataTable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Data;&lt;br /&gt;
class Program {&lt;br /&gt;
    static void Main(string[] args) {&lt;br /&gt;
        DataSet carsInventoryDS = new DataSet(&amp;quot;Inventory&amp;quot;);&lt;br /&gt;
        carsInventoryDS.ExtendedProperties[&amp;quot;TimeStamp&amp;quot;] = DateTime.Now;&lt;br /&gt;
        carsInventoryDS.ExtendedProperties[&amp;quot;Company&amp;quot;] = &amp;quot;Name&amp;quot;;&lt;br /&gt;
        DataColumn carIDColumn = new DataColumn(&amp;quot;CarID&amp;quot;, typeof(int));&lt;br /&gt;
        carIDColumn.ReadOnly = true;&lt;br /&gt;
        carIDColumn.Caption = &amp;quot;Car ID&amp;quot;;&lt;br /&gt;
        carIDColumn.AllowDBNull = false;&lt;br /&gt;
        carIDColumn.Unique = true;&lt;br /&gt;
        carIDColumn.AutoIncrement = true;&lt;br /&gt;
        carIDColumn.AutoIncrementSeed = 0;&lt;br /&gt;
        carIDColumn.AutoIncrementStep = 1;&lt;br /&gt;
        carIDColumn.ColumnMapping = MappingType.Attribute;&lt;br /&gt;
        DataColumn carMakeColumn = new DataColumn(&amp;quot;Make&amp;quot;, typeof(string));&lt;br /&gt;
        DataColumn carColorColumn = new DataColumn(&amp;quot;Color&amp;quot;, typeof(string));&lt;br /&gt;
        DataColumn carPetNameColumn = new DataColumn(&amp;quot;PetName&amp;quot;, typeof(string));&lt;br /&gt;
        carPetNameColumn.Caption = &amp;quot;Name&amp;quot;;&lt;br /&gt;
        DataTable inventoryTable = new DataTable(&amp;quot;Inventory&amp;quot;);&lt;br /&gt;
        inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });&lt;br /&gt;
        inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };&lt;br /&gt;
        DataRow carRow = inventoryTable.NewRow();&lt;br /&gt;
        carRow[&amp;quot;Make&amp;quot;] = &amp;quot;B&amp;quot;;&lt;br /&gt;
        carRow[&amp;quot;Color&amp;quot;] = &amp;quot;C&amp;quot;;&lt;br /&gt;
        carRow[&amp;quot;PetName&amp;quot;] = &amp;quot;A&amp;quot;;&lt;br /&gt;
        inventoryTable.Rows.Add(carRow);&lt;br /&gt;
        carRow = inventoryTable.NewRow();&lt;br /&gt;
        carRow[&amp;quot;Make&amp;quot;] = &amp;quot;S&amp;quot;;&lt;br /&gt;
        carRow[&amp;quot;Color&amp;quot;] = &amp;quot;R&amp;quot;;&lt;br /&gt;
        carRow[&amp;quot;PetName&amp;quot;] = &amp;quot;E&amp;quot;;&lt;br /&gt;
        inventoryTable.Rows.Add(carRow);&lt;br /&gt;
        carsInventoryDS.Tables.Add(inventoryTable);&lt;br /&gt;
        PrintTable(carsInventoryDS.Tables[&amp;quot;Inventory&amp;quot;]);&lt;br /&gt;
    }&lt;br /&gt;
    private static void PrintTable(DataTable dt) {&lt;br /&gt;
        DataTableReader dtReader = dt.CreateDataReader();&lt;br /&gt;
        while (dtReader.Read()) {&lt;br /&gt;
            for (int i = 0; i &amp;lt; dtReader.FieldCount; i++) {&lt;br /&gt;
                Console.Write(&amp;quot;{0} = {1} &amp;quot;,&lt;br /&gt;
                    dtReader.GetName(i).Trim(),&lt;br /&gt;
                    dtReader.GetValue(i).ToString().Trim());&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine();&lt;br /&gt;
        }&lt;br /&gt;
        dtReader.Close();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>