using System;
using System.Data;
using System.Data.SqlClient;
class ModifyDataTable
{
static void Main(string[] args)
{
string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
string sql = @"select * from employee where country = 'UK'";
SqlConnection conn = new SqlConnection(connString);
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "employees");
DataTable dt = ds.Tables["employees"];
dt.Columns["firstname"].AllowDBNull = true;
dt.Rows[0]["city"] = "w";
DataRow newRow = dt.NewRow();
newRow["firstname"] = "R";
newRow["lastname"] = "B";
newRow["titleofcourtesy"] = "Sir";
newRow["city"] = "B";
newRow["country"] = "USA";
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"]);
}
}
catch(Exception e)
{
Console.WriteLine("Error: " + e);
}
finally
{
conn.Close();
}
}
}
|