Presentation is loading. Please wait.

Presentation is loading. Please wait.

 José A. Blakeley Partner Architect Microsoft Corporation  Michael Pizzo Principal Architect Microsoft Corporation.

Similar presentations


Presentation on theme: " José A. Blakeley Partner Architect Microsoft Corporation  Michael Pizzo Principal Architect Microsoft Corporation."— Presentation transcript:

1  José A. Blakeley Partner Architect Microsoft Corporation  Michael Pizzo Principal Architect Microsoft Corporation

2

3

4

5

6

7

8

9

10 Managed Provider DataReader Command Connection Controls, Designers, Code-gen, etc DataSet DataAdapter XmlReaderXmlWriter OLTP operations, Programmatic Processing, Frameworks

11 Data store Data Provider Connection CreateCommand() ExecuteReader() DataReader Command Parameters

12 DataSet Tables Table Columns Column Constraints Constraint Rows Row Relations Relation

13

14 Data store DataAdapter Mappings InsertCommand UpdateCommand DeleteCommand SelectCommand Fill()Update() DataSet

15

16

17  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

18

19

20

21 The Need… Applications work with a well Defined Model Storage Schema Abstraction Declarative mapping between application and storage models No brittle, hard-coded mapping

22 SalesPerson EmployeeID = LoginID = pete Title = "Developer" VacationHours = 0 … ExpenseAccount = … CarLicenseNum = … … SalesPerson EmployeeID = LoginID = pete Title = "Developer" VacationHours = 0 … ExpenseAccount = … CarLicenseNum = … … SalesPerson EmployeeID = LoginID = pete Title = "Developer" VacationHours = 0 … ExpenseAccount = true … SalesPerson EmployeeID = LoginID = adam Title = "Dev Lead" VacationHours = 0 … Reports Manager 1 1 N

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40 // 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 (this T[] names, Func 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 / };

41

42

43

44

45

46

47

48

49

50

51

52 var query = from row in myDataSet.Tables["Customers"].AsEnumerable() where row.Field ("City") == "London" where row.Field ("City") == "London" select new { row.Field ("CustomerID"), select new { row.Field ("CustomerID"), row.Field ("ContactName") } ; var query = from customer in northwind.Customers where customer.City == "London" select customer; Typed DataSet Use strongly typed accessors

53

54

55

56

57

58 Customizing Data Classes

59

60  Customizing Data Classes  Entity Framework Mapping Scenarios  Core Mapping Scenarios  Function Mapping  Mapping Limitations Database Design Considerations Advanced Mapping Techniques

61

62

63

64

65

66

67

68

69

70

71 Use your ComplexType in your Entities Map the Complex Type in your MSL

72

73 Customize Conceptual Definition

74 Customize Mapping Definition

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90 CREATE TYPE myTableType AS TABLE (id INT, name NVARCHAR(100),qty INT); CREATE PROCEDURE myProc myTableType READONLY) AS UPDATE Inventory SET qty += s.qty FROM Inventory AS i INNER AS tvp ON i.id = tvp.id GO

91

92 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 = dt); //Execute command tvp_cmd.ExecuteNonQuery();

93

94 SqlCommand command = new SqlCommand(string.Empty, sqlConnection); command.CommandText = "insert …. // 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); ….

95 // create a command to get the DueDate SqlCommand command = new SqlCommand(String.Empty, sqlConnection); command.CommandText = "select DueDate from MoviesRented where 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); }

96

97 // 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); }

98

99

100 public SqlDataReader GetProducts(int Category) { SqlCommand cmd = new SqlCommand( "Select ProductName, UnitPrice from Products " + "where CategoryID cnn); cmd.Notification = new SqlNotificationRequest( Category.ToString(),// message "myQueue", // message body 3000);// timeout return cmd.Execute(); } public void WaitForChanges() { SqlCommand cmd = new SqlCommand( "Receive message_body from myQueue " + "WITH wait_for_results", cnn); cmd.CommandTimeout = 0; int category = (int)cmd.ExecuteScalar(); Console.WriteLine("Category {0} changed.",category); }

102

103 SqlConnection cnn = new SqlConnection(connectString); cnn.Open(); SqlCommand cmd = new SqlCommand("SELECT p FROM PointTable", cnn ); SqlDataReader reader = cmd.ExecuteReader(); while( reader.Read() ) { Point point=(Point)reader[0]; Console.WriteLine( "x:{0}, y:{1}", point.x, point.y ); } cnn.Close();

104 Aggregates AVG CHECKSUM_AGG COUNT COUNT_BIG MAX MIN STDEV STDEVP VAR VARP String Functions ASCII CHAR CHARINDEX DIFFERENCE LEFT LEN LOWER LTRIM nchar PATINDEX QUOTENAME REPLACE REPLICATE REVERSE RIGHT RTRIM SOUNDEX SPACE STR STUFF SUBSTRING UNICODE UPPER Math Functions ABS ACOS ASIN ATAN ATN2 CEILING COS COT DEGREES EXP FLOOR LOG LOG10 PI POWER RADIANS RAND ROUND SIGN SIN SQRT SQUARE TAN Date Functions DATEADD DATEDIFF DATENAME DATEPART DAY GETDATE SYSDATETIME SYSUTCDATETIME SYSDATETIMEOFFSET GETUTCDATE MONTH YEAR System Functions DATALENGTH CHECKSUM NEWID CURRENT_TIMESTAMP CURRENT_USER HOST_NAME USER_NAME ISNUMERIC ISDATE

105

106

107

108

109

110

111

112

113

114 © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt " José A. Blakeley Partner Architect Microsoft Corporation  Michael Pizzo Principal Architect Microsoft Corporation."

Similar presentations


Ads by Google