Use DataTable to insert a Row : DataTable : ADO.Net C# Examples


C# Examples » ADO.Net » DataTable »

 

Use DataTable to insert a Row









    
using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;
using  System.Data.SqlClient;

public  class  MainClass  {
        [STAThread]
        static  void  Main()  
        {
                string  ConnectionString  ="server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated  Security=SSPI;";
                SqlConnection  conn  =  new  SqlConnection(ConnectionString);

                conn.Open();

                SqlDataAdapter  adapter  =  new  SqlDataAdapter("SELECT  *  FROM  Employee  ORDER  BY  ID",  conn);

                SqlCommandBuilder  builder  =  new  SqlCommandBuilder(adapter);

                //  Create  a  dataset  object
                DataSet  ds  =  new  DataSet("EmployeeSet");
                adapter.Fill(ds,  "Employee");

                //  Create  a  data  table  object  and  add  a  new  row
                DataTable  EmployeeTable  =  ds.Tables["Employee"];
                DataRow  row  =  EmployeeTable.NewRow();
                row["FirstName"]  =  "R";
                row["LastName"]  =  "D";
                row["ID"]  =  "10";
                EmployeeTable.Rows.Add(row);

                //  Update  data  adapter
                adapter.Update(ds,  "Employee");

                Console.WriteLine(row["FirstName"].ToString().Trim()  +  "  "  +  row["LastName"].ToString().Trim()  +  "  added  to  Employees");
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» DataTable