33
44using System ;
55using System . Collections . Generic ;
6- using System . Data . SqlClient ;
6+ using Microsoft . Data . SqlClient ;
77using Dapper ;
88using DapperExtensions ;
99using Microsoft . Azure . SqlDatabase . ElasticScale . ShardManagement ;
@@ -67,89 +67,74 @@ public static void Main()
6767 Console . Write ( "Enter a name for a new Blog: " ) ;
6868 var name = Console . ReadLine ( ) ;
6969
70- SqlDatabaseUtils . SqlRetryPolicy . ExecuteAction ( ( ) =>
70+ using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
71+ key : s_tenantId1 ,
72+ connectionString : connStrBldr . ConnectionString ,
73+ options : ConnectionOptions . Validate ) )
7174 {
72- using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
73- key : s_tenantId1 ,
74- connectionString : connStrBldr . ConnectionString ,
75- options : ConnectionOptions . Validate ) )
76- {
77- var blog = new Blog { Name = name } ;
78- sqlconn . Insert ( blog ) ;
79- }
80- } ) ;
75+ var blog = new Blog { Name = name } ;
76+ sqlconn . Insert ( blog ) ;
77+ }
8178
82- SqlDatabaseUtils . SqlRetryPolicy . ExecuteAction ( ( ) =>
79+ using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
80+ key : s_tenantId1 ,
81+ connectionString : connStrBldr . ConnectionString ,
82+ options : ConnectionOptions . Validate ) )
8383 {
84- using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
85- key : s_tenantId1 ,
86- connectionString : connStrBldr . ConnectionString ,
87- options : ConnectionOptions . Validate ) )
84+ // Display all Blogs for tenant 1
85+ IEnumerable < Blog > result = sqlconn . Query < Blog > ( @"
86+ SELECT *
87+ FROM Blog
88+ ORDER BY Name" ) ;
89+
90+ Console . WriteLine ( "All blogs for tenant id {0}:" , s_tenantId1 ) ;
91+ foreach ( var item in result )
8892 {
89- // Display all Blogs for tenant 1
90- IEnumerable < Blog > result = sqlconn . Query < Blog > ( @"
91- SELECT *
92- FROM Blog
93- ORDER BY Name" ) ;
94-
95- Console . WriteLine ( "All blogs for tenant id {0}:" , s_tenantId1 ) ;
96- foreach ( var item in result )
97- {
98- Console . WriteLine ( item . Name ) ;
99- }
93+ Console . WriteLine ( item . Name ) ;
10094 }
101- } ) ;
95+ }
10296
10397 // Do work for tenant 2 :-)
10498 // Here I am going to illustrate how to integrate
10599 // with DapperExtensions which saves us the T-SQL
106100 //
107- SqlDatabaseUtils . SqlRetryPolicy . ExecuteAction ( ( ) =>
101+ using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
102+ key : s_tenantId2 ,
103+ connectionString : connStrBldr . ConnectionString ,
104+ options : ConnectionOptions . Validate ) )
108105 {
109- using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
110- key : s_tenantId2 ,
111- connectionString : connStrBldr . ConnectionString ,
112- options : ConnectionOptions . Validate ) )
106+ // Display all Blogs for tenant 2
107+ IEnumerable < Blog > result = sqlconn . GetList < Blog > ( ) ;
108+ Console . WriteLine ( "All blogs for tenant id {0}:" , s_tenantId2 ) ;
109+ foreach ( var item in result )
113110 {
114- // Display all Blogs for tenant 2
115- IEnumerable < Blog > result = sqlconn . GetList < Blog > ( ) ;
116- Console . WriteLine ( "All blogs for tenant id {0}:" , s_tenantId2 ) ;
117- foreach ( var item in result )
118- {
119- Console . WriteLine ( item . Name ) ;
120- }
111+ Console . WriteLine ( item . Name ) ;
121112 }
122- } ) ;
113+ }
123114
124115 // Create and save a new Blog
125116 Console . Write ( "Enter a name for a new Blog: " ) ;
126117 var name2 = Console . ReadLine ( ) ;
127118
128- SqlDatabaseUtils . SqlRetryPolicy . ExecuteAction ( ( ) =>
119+ using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
120+ key : s_tenantId2 ,
121+ connectionString : connStrBldr . ConnectionString ,
122+ options : ConnectionOptions . Validate ) )
129123 {
130- using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey (
131- key : s_tenantId2 ,
132- connectionString : connStrBldr . ConnectionString ,
133- options : ConnectionOptions . Validate ) )
134- {
135- var blog = new Blog { Name = name2 } ;
136- sqlconn . Insert ( blog ) ;
137- }
138- } ) ;
124+ var blog = new Blog { Name = name2 } ;
125+ sqlconn . Insert ( blog ) ;
126+ }
139127
140- SqlDatabaseUtils . SqlRetryPolicy . ExecuteAction ( ( ) =>
128+ using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey ( s_tenantId2 , connStrBldr . ConnectionString , ConnectionOptions . Validate ) )
141129 {
142- using ( SqlConnection sqlconn = shardingLayer . ShardMap . OpenConnectionForKey ( s_tenantId2 , connStrBldr . ConnectionString , ConnectionOptions . Validate ) )
130+ // Display all Blogs for tenant 2
131+ IEnumerable < Blog > result = sqlconn . GetList < Blog > ( ) ;
132+ Console . WriteLine ( "All blogs for tenant id {0}:" , s_tenantId2 ) ;
133+ foreach ( var item in result )
143134 {
144- // Display all Blogs for tenant 2
145- IEnumerable < Blog > result = sqlconn . GetList < Blog > ( ) ;
146- Console . WriteLine ( "All blogs for tenant id {0}:" , s_tenantId2 ) ;
147- foreach ( var item in result )
148- {
149- Console . WriteLine ( item . Name ) ;
150- }
135+ Console . WriteLine ( item . Name ) ;
151136 }
152- } ) ;
137+ }
153138
154139 Console . WriteLine ( "Press any key to exit..." ) ;
155140 Console . ReadKey ( ) ;
@@ -168,6 +153,7 @@ private static void CreateSchema(string shardName)
168153
169154 using ( SqlConnection conn = new SqlConnection ( connStrBldr . ToString ( ) ) )
170155 {
156+ conn . RetryLogicProvider = SqlDatabaseUtils . SqlRetryProvider ;
171157 conn . Open ( ) ;
172158 conn . Execute ( @"
173159 IF (OBJECT_ID('[dbo].[Blog]', 'U') IS NULL)
0 commit comments