Lecture 11 - ADO.NET (Part 2)
Lecture 11 - ADO.NET (Part 2)
6 Framework
}
}
A Simple Data Adapter Example
public static void Main(string[] args)
{
string conStr = $"Server=localhost;Port=8889;Database=AutoLot;User=root;Password=root";
DataSet ds = new DataSet();
DbDataAdapter adapter = new MySqlDataAdapter("select * from inventory", conStr);
adapter.Fill(ds, "Inventory");
PrintDataSet(ds);
}
Mapping Database Names to Friendly
Names
public static void Main(string[] args)
{
string conStr = $"Server=localhost;Port=8889;Database=AutoLot;User=root;Password=root";
DataSet ds = new DataSet();
DbDataAdapter adapter = new MySqlDataAdapter("select * from Inventory", conStr);
adapter.TableMappings.Add("Inventory", "Current Inventory");
DataTableMapping tableMapping = adapter.TableMappings["Inventory"];
tableMapping.ColumnMappings.Add("CarId", "Car ID");
tableMapping.ColumnMappings.Add("PetName", "Name of car");
adapter.Fill(ds, "Inventory");
PrintDataSet(ds);
}
PROGRAMMING WITH LINQ TO
DATASET
LINQ over DataTable
public static void Main(string[] args)
{
string conStr = $"Server=localhost;Port=8889;Database=AutoLot;User=root;Password=root";
DataSet ds = new DataSet();
DbDataAdapter adapter = new MySqlDataAdapter("select * from Inventory", conStr);
adapter.Fill(ds, "Inventory");
We cannot use Linq over DataTable directly.
//PrintDataSet(ds);
DataTable tbl = ds.Tables[0]; We need to convert to Enumerable
var subset = from car in tbl.AsEnumerable() We need to add System.Data.DataSetExtensions to
where ((string)car["Color"]).Equals("Black") enable this
select car;
foreach (var item in subset)
{
Console.WriteLine($"{item["PetName"]} is {item["Color"]}");
}
Console.ReadLine();
}
The DataRowExtensions.Field<T>()
Extension Method
• LINQ query expression without .Field<T>()
– Uses numerous casting operations and DataRow
indexers to gather the result set
– Could result in runtime exceptions if you attempt
to cast to an incompatible data type
• Use of DataRow.Field<T>()
– To inject some strong typing into your query
– To increase the type safety of your query
– Data type is checked at Compile time
LINQ over DataTable
public static void Main(string[] args)
{
string conStr = $"Server=localhost;Port=8889;Database=AutoLot;User=root;Password=root";
DataSet ds = new DataSet();
DbDataAdapter adapter = new MySqlDataAdapter("select * from Inventory", conStr);
adapter.Fill(ds, "Inventory");
//PrintDataSet(ds);
DataTable tbl = ds.Tables[0];
var cars = from car in tbl.AsEnumerable()
where car.Field<string>("Color") == "Black"
select new
{ DataRow.Field<T> will help to check the type at
ID = car.Field<int>("CarID"), compile time
Make = car.Field<string>("Make"),
Color = car.Field<string>("Color")
};
foreach (var item in cars)
{
Console.WriteLine($"Car {item.ID} is a {item.Color} {item.Make}");
}
Console.ReadLine();
}
Hydrating New DataTables from LINQ
Queries
public static void Main(string[] args)
{
string conStr = $"Server=localhost;Port=8889;Database=AutoLot;User=root;Password=root";
DataSet ds = new DataSet();
DbDataAdapter adapter = new MySqlDataAdapter("select * from Inventory", conStr);
adapter.Fill(ds, "Inventory");
//PrintDataSet(ds);
DataTable tbl = ds.Tables[0];
var cars = from car in tbl.AsEnumerable()
where car.Field<string>("Color") == "Black"
select car; This is an extension method from
DataTable newTbl = cars.CopyToDataTable(); System.Data.DataSetExtensions
foreach (DataRow row in newTbl.Rows)
{
foreach (DataColumn col in newTbl.Columns)
{
Console.Write($"{row[col]}\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
Design for DataSet
• In class tutorial about typed dataset
– Design DataSet
– Fill data
– Insert data using typed DataSet
Summary
• Understanding the Disconnected Layer of
ADO.NET
• Understanding the Role of the DataSet
• Understanding DataColumns
• Understanding DataRows
• Understanding DataTables
• Working with Data Adapters
• Programming with LINQ to DataSet
References
Troelsen, A. & Japikse, P., 2015. C# 6 and the
.NET 4.6 Framework. 7th ed. Apress.