9. Evolution of Data Access APIs
Getting Data from a SQL Database
Working with Data
ADO.NET and XML
Building a Data Platform
The ADO.NET Entity Framework
16. Evolution of Data Access APIs
ADO.NET 1.0
Building a Data Platform
Why a Conceptual Model?
The Microsoft Entity Data Model
Entity SQL
The ADO.NET Entity Framework
20.
The Need…
Applications work with a well
Defined Model
Storage Schema Abstraction
Declarative mapping between
application and storage models
No brittle, hard-coded mapping
34. // Lambda Expressions
string[] names = { "Luis", "Mary", "Mike", "Jose" };
Display( names, s => s.Length > 3);
// Anonymous Types and object initialization
var emp = new { Name = "Mary", Company = "Microsoft",
Age = 30 };
// Extension Methods
public static class ExtensionMethods {
public static void Display<T>(this T[] names,
Func<T, bool> filter) {
foreach (T s in names) {
if (filter(s)) Console.WriteLine(s);
}
}
}
// Query Expressions
var query = from c in Customers
where c.Discount >= 3.0 && c.Discount < 4.0
select new { c.Name, Perc = c.Discount / 100.0 };
72.
Simple programming model
Strongly typed
Reduce client/server round trips
Do not cause a statement to recompile
CREATE TYPE myTableType AS TABLE
(id INT, name NVARCHAR(100),qty
INT);
CREATE PROCEDURE myProc (@tvp
myTableType READONLY) AS
UPDATE Inventory SET
qty += s.qty
FROM Inventory AS i INNER JOIN
@tvp AS tvp
ON i.id = tvp.id
GO
74. ADO.NET Example using DataTable
Using (MyConnection){
//Create a data table
DataTable dt = new DataTable(“TVPOrdersDataTable”);
dt.Columns.Add(“ProductType”, typeof(string));
dt.Columns.Add(“Quantity”, typeof(int));
// Add rows
dt.Rows.Add(“Canon Digital Camera”, 20);
dt.Rows.Add(“June”, 10);
dt.Rows.Add(“Xbox-360”, 8);
// Create a command and bind parameter
SqlCommand tvp_cmd = new
SqlCommand(“sp_UpdataInventory”,
MyConnection);
SqlParameter tvpParam =
tvp_cmd.Parameters.AddWithValue(
@OrdersTvp, dt);
//Execute command
tvp_cmd.ExecuteNonQuery();
75.
76. SqlCommand command =
new SqlCommand(string.Empty, sqlConnection);
command.CommandText = "insert into MoviesRented
values(@customerId, @MovieID, @RentalDate,
@DueDate)";
….
// create a parameter for RentalDate
SqlParameter rentDateParam = new SqlParameter("RentDate",
System.Data.SqlDbType.DateTimeOffset);
rentDateParam.Value = DateTimeOffset.Now;
command.Parameters.Add(rentDateParam);
// create a parameter for DueDate
SqlParameter dueDateParam = new SqlParameter("DueDate",
System.Data.SqlDbType.DateTimeOffset);
dueDateParam.Value = DateTimeOffset.Now.AddDays(7);
command.Parameters.Add(dueDateParam);
….
77. // create a command to get the DueDate
SqlCommand command =
new SqlCommand(String.Empty, sqlConnection);
command.CommandText =
"select DueDate from MoviesRented where MovieId = @MovieId";
…
// Execute the DataReader
//
using (SqlDataReader dataReader = command.ExecuteReader())
{
if (dataReader.Read() == false)
{
Console.WriteLine("Movie has not been rented");
}
DateTimeOffset dueDate =
dataReader.GetDateTimeOffset (0);
Console.WriteLine("Movie due back on : {0}", dueDate);
}
79. // Poll for completion
IAsyncResult result = cmd.BeginExecuteReader();
while(!result.IsCompleted) {
// do some work
}
SqlDataReader reader = cmd.EndExecuteReader(result);
// Use a Callback
IAsyncResult result = cmd.ExecuteReader(
new AsyncCallback( myDataCallback ));
// do other work…
// optionally wait using sync object
result.WaitHandle.WaitOne();
public void myDataCallback( IAsyncResult result ) {
SqlDataReader reader = cmd.EndExecuteReader(result);
}