Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Persist Adds Builder

using System; using System.Data; using System.Data.SqlClient;     class PersistAddsBuilder     {         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"] = "Roy";                 newRow["lastname"] = "Beatty";                 newRow["titleofcourtesy"] = "Sir";                 newRow["city"] = "Birmingham";                 newRow["country"] = "UK";                 dt.Rows.Add(newRow);                 foreach (DataRow row in dt.Rows)                 {                     Console.WriteLine(row["firstname"]);                     Console.WriteLine(row["lastname"]);                     Console.WriteLine(row["city"]);                 }                 da.Update(ds, "employees");             }             catch (Exception e)             {                 Console.WriteLine("Error: " + e);             }             finally             {                 conn.Close();             }         }     }