Database Reference
In-Depth Information
Listing 15-5. PersistAdds.cs
Using System.Data.SqlClient;
private void PersistAdds_Load(object sender, EventArgs e)
{
// Connection string
string connString = @" server=.\sql2012;database=AdventureWorks;Integrated
Security=true";
// Query
string qry = @" select *
from HumanResources.Department
where GroupName = 'Sales'";
// SQL to insert employees
string ins = @"insert into HumanResources.Department
(Name,GroupName, ModifiedDate)
values(@Name, @GroupName, @ModifiedDate)";
// Create connection
SqlConnection conn = new SqlConnection(connString);
try
{
// Create data adapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry, conn);
// Create and fill data set
DataSet ds = new DataSet();
da.Fill(ds, "HumanResources.Department");
// Get data table reference
DataTable dt = ds.Tables["HumanResources.Department"];
// Add a row
DataRow newRow = dt.NewRow();
newRow["Name"] = "Microsoft Development";
newRow["GroupName"] = "Global Development";
newRow["ModifiedDate"] = "2012-04-28";
dt.Rows.Add(newRow);
// Display rows
foreach (DataRow row in dt.Rows)
{
txtDepartment.AppendText(row["Name"].ToString());
txtDepartment.AppendText("\t");
txtDepartment.AppendText(row["GroupName"].ToString());
txtDepartment.AppendText("\t");
txtDepartment.AppendText(row["ModifiedDate"].ToString());
Search WWH ::




Custom Search