using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
class Program {
static void Main(string[] args) {
DataSet theCarsInventory = new DataSet();
SqlConnection cn = new SqlConnection("server=(local);User ID=sa;Pwd=;database=Cars");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Inventory", cn);
SqlCommandBuilder invBuilder = new SqlCommandBuilder(da);
da.Fill(theCarsInventory, "Inventory");
PrintDataSet(theCarsInventory);
try {
theCarsInventory.Tables["Inventory"].Rows[1].Delete();
da.Update(theCarsInventory, "Inventory");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
theCarsInventory = new DataSet();
da.Fill(theCarsInventory, "Inventory");
PrintDataSet(theCarsInventory);
}
static void PrintDataSet(DataSet ds) {
Console.WriteLine("Tables in '{0}' DataSet.\n", ds.DataSetName);
foreach (DataTable dt in ds.Tables) {
Console.WriteLine("{0} Table.\n", dt.TableName);
for (int curCol = 0; curCol < dt.Columns.Count; curCol++) {
Console.Write(dt.Columns[curCol].ColumnName.Trim() + "\t");
}
for (int curRow = 0; curRow < dt.Rows.Count; curRow++) {
for (int curCol = 0; curCol < dt.Columns.Count; curCol++) {
Console.Write(dt.Rows[curRow][curCol].ToString().Trim() + "\t");
}
Console.WriteLine();
}
}
}
}
|