@@ -26,87 +26,91 @@ Result: sort in memory|Yes|No
2626
2727## [ Object-relational mapping (ORM)] ( https://en.wikipedia.org/wiki/Object-relational_mapping )
2828
29- 1 . Download and restore the SQL Server 2016 sample database from https://github.com/microsoft/sql-server-samples
30- 2 . Open the database with Data Commander and execute the following command:
29+ Download and restore the SQL Server 2016 sample database from https://github.com/microsoft/sql-server-samples
3130
32- ``` SQL
33- use WideWorldImporters
34-
35- select top 0
36- CustomerID,
37- CustomerName,
38- BillToCustomerID,
39- CustomerCategoryID
40- from Sales .Customers
31+ Open the database with Data Commander and execute the following command:
4132
42- select top 0 *
43- from Sales .CustomerTransactions
33+ ``` SQL
34+ select
35+ c .CustomerID ,
36+ c .CustomerName
37+ from Sales .Customers c
38+
39+ select
40+ i .InvoiceID ,
41+ i .CustomerID ,
42+ i .InvoiceDate
43+ from Sales .Invoices i
44+ order by i .CustomerID ,i .InvoiceID
4445```
4546
46- 3 . The program generates C# ORM code snippets. See them in the Messages window :
47+ The program generates C# ORM code snippets. See them in log file :
4748
4849``` C#
49- internal sealed class Row0
50+ internal sealed class Customer
5051{
51- public int CustomerID ;
52- public string CustomerName ;
53- public int BillToCustomerID ;
54- public int CustomerCategoryID ;
52+ public int CustomerID ;
53+ public string CustomerName ;
5554}
5655
57- private static Row0 Read0 (IDataRecord dataRecord )
56+ private static Customer ReadCustomer (IDataRecord dataRecord )
5857{
59- var row = new Row0 ();
60- row .CustomerID = dataRecord .GetInt32 (0 );
61- row .CustomerName = dataRecord .GetString (1 );
62- row .BillToCustomerID = dataRecord .GetInt32 (2 );
63- row .CustomerCategoryID = dataRecord .GetInt32 (3 );
64- return row ;
58+ var @object = new Customer ();
59+ @object .CustomerID = dataRecord .GetInt32 (0 );
60+ @object .CustomerName = dataRecord .GetString (1 );
61+ return @object ;
6562}
6663
67- internal sealed class Row1
64+ internal sealed class Invoice
6865{
69- public int CustomerTransactionID ;
70- public int CustomerID ;
71- public int TransactionTypeID ;
72- public int ? InvoiceID ;
73- public int ? PaymentMethodID ;
74- public DateTime TransactionDate ;
75- public decimal AmountExcludingTax ;
76- public decimal TaxAmount ;
77- public decimal TransactionAmount ;
78- public decimal OutstandingBalance ;
79- public DateTime ? FinalizationDate ;
80- public bool ? IsFinalized ;
81- public int LastEditedBy ;
82- public DateTime LastEditedWhen ;
66+ public int InvoiceID ;
67+ public int CustomerID ;
68+ public DateTime InvoiceDate ;
8369}
8470
85- private static Row1 Read1 (IDataRecord dataRecord )
71+ private static Invoice ReadInvoice (IDataRecord dataRecord )
8672{
87- var row = new Row1 ();
88- row .CustomerTransactionID = dataRecord .GetInt32 (0 );
89- row .CustomerID = dataRecord .GetInt32 (1 );
90- row .TransactionTypeID = dataRecord .GetInt32 (2 );
91- row .InvoiceID = dataRecord .GetNullableInt32 (3 );
92- row .PaymentMethodID = dataRecord .GetNullableInt32 (4 );
93- row .TransactionDate = dataRecord .GetDateTime (5 );
94- row .AmountExcludingTax = dataRecord .GetDecimal (6 );
95- row .TaxAmount = dataRecord .GetDecimal (7 );
96- row .TransactionAmount = dataRecord .GetDecimal (8 );
97- row .OutstandingBalance = dataRecord .GetDecimal (9 );
98- row .FinalizationDate = dataRecord .GetNullableDateTime (10 );
99- row .IsFinalized = dataRecord .GetNullableBoolean (11 );
100- row .LastEditedBy = dataRecord .GetInt32 (12 );
101- row .LastEditedWhen = dataRecord .GetDateTime (13 );
102- return row ;
73+ var @object = new Invoice ();
74+ @object .InvoiceID = dataRecord .GetInt32 (0 );
75+ @object .CustomerID = dataRecord .GetInt32 (1 );
76+ @object .InvoiceDate = dataRecord .GetDateTime (2 );
77+ return @object ;
10378}
79+ ```
80+
81+ 2 . The generated code can be used like this:
82+
83+ ``` C#
84+ var connectionStringBuilder = new SqlConnectionStringBuilder ();
85+ connectionStringBuilder .DataSource = @" .\SQL2016_001" ;
86+ connectionStringBuilder .InitialCatalog = " WideWorldImporters" ;
87+ connectionStringBuilder .IntegratedSecurity = true ;
10488
105- private static ExecuteReaderResponse < Row0 , Row1 > Execute ( this IDbCommandExecutor executor )
89+ using ( var connection = new SqlConnection ( connectionStringBuilder . ConnectionString ) )
10690{
107- const string commandText = " ..." ;
108- var request = new ExecuteReaderRequest (commandText );
109- return executor .Read (request ,Read0 ,Read1 );
91+ connection .Open ();
92+ var executor = connection .CreateCommandAsyncExecutor ();
93+
94+ var commandText = " waitfor delay '00:00:01'" ;
95+ var affectedRows = await executor .ExecuteNonQueryAsync (new ExecuteNonReaderRequest (commandText ));
96+
97+ commandText = " select top 1 i.InvoiceID from Sales.Invoices i" ;
98+ var scalar = await executor .ExecuteScalarAsync (new ExecuteNonReaderRequest (commandText ));
99+
100+ var commandText = @" select
101+ c.CustomerID,
102+ c.CustomerName
103+ from Sales.Customers c
104+
105+ select
106+ i.InvoiceID,
107+ i.CustomerID,
108+ i.InvoiceDate
109+ from Sales.Invoices i
110+ order by i.CustomerID,i.InvoiceID" ;
111+ var response = await executor .ExecuteReaderAsync (new ExecuteReaderRequest (commandText ), ReadCustomer , ReadInvoice );
112+ var customers = response .Objects1 ;
113+ var invoices = response .Objects2 ;
110114}
111115
112116```
0 commit comments