Update SqlDataAdapter with DataSet : SqlDataAdapter : ADO.Net C# Examples


C# Examples » ADO.Net » SqlDataAdapter »

 

Update SqlDataAdapter with DataSet









    
using  System;
using  System.Data;
using  System.Data.SqlClient;

class  MainClass
{
      static  void  Main(string[]  args)
      {
            string  connString  =  @"server  =  .\sqlexpress;integrated  security  =  true;database  =  northwind";

            string  qry  =  @"select  *  from  employees  where  country  =  'UK'";

            SqlConnection  conn  =  new  SqlConnection(connString);

            try
            {
                  SqlDataAdapter  da  =  new  SqlDataAdapter();
                  da.SelectCommand  =  new  SqlCommand(qry,  conn);

                  SqlCommandBuilder  cb  =  new  SqlCommandBuilder(da);

                  DataSet  ds  =  new  DataSet();
                  da.Fill(ds,  "employees");

                  DataTable  dt  =  ds.Tables["employees"];

                  DataRow  newRow  =  dt.NewRow();
                  newRow["firstname"]  =  "R";
                  newRow["lastname"]  =  "B";
                  newRow["titleofcourtesy"]  =  "Sir";
                  newRow["city"]  =  "B";
                  newRow["country"]  =  "UK";
                  dt.Rows.Add(newRow);

                  foreach  (DataRow  row  in  dt.Rows)
                  {
                        Console.WriteLine(
                              "{0}  {1}  {2}",
                              row["firstname"].ToString().PadRight(15),
                              row["lastname"].ToString().PadLeft(25),
                              row["city"]);
                  }
                  da.Update(ds,  "employees");
            }
            catch(Exception  e)
            {
                  Console.WriteLine("Error:  "  +  e);
            }
            finally
            {
                  conn.Close();
            }
      }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlDataAdapter