1
- // <Snippet1>
1
+ // <Snippet1>
2
2
using System ;
3
3
using System . Data . SqlClient ;
4
4
using System . Data ;
8
8
namespace CSDataIsolationLevel {
9
9
10
10
// Use the delegate to call the different threads.
11
- public delegate void AsyncAccessDatabase ( String connString , IsolationLevel level ) ;
11
+ public delegate void AsyncAccessDatabase ( string connString , IsolationLevel level ) ;
12
12
13
13
static class DirtyReadThreads {
14
- public static void DirtyReadFirstThread ( String connStrig , IsolationLevel level ) {
14
+ public static void DirtyReadFirstThread ( string connStrig , IsolationLevel level ) {
15
15
Console . WriteLine ( "Begin the DirtyReadFirstThread....." ) ;
16
16
17
17
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
18
- String cmdText = @"Use DbDataIsolationLevel;
18
+ string cmdText = @"Use DbDataIsolationLevel;
19
19
20
20
Update dbo.Products set Quantity=Quantity+100 where ProductId=1;
21
21
WaitFor Delay '00:00:06';" ;
@@ -36,11 +36,11 @@ public static void DirtyReadFirstThread(String connStrig, IsolationLevel level)
36
36
Console . WriteLine ( "Exit from the DirtyReadFirstThread....." ) ;
37
37
}
38
38
39
- public static void DirtyReadSecondThread ( String connStrig , IsolationLevel level ) {
39
+ public static void DirtyReadSecondThread ( string connStrig , IsolationLevel level ) {
40
40
Console . WriteLine ( "Begin the DirtyReadSecondThread....." ) ;
41
41
42
42
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
43
- String cmdText = @"Use DbDataIsolationLevel;
43
+ string cmdText = @"Use DbDataIsolationLevel;
44
44
45
45
WaitFor Delay '00:00:03';
46
46
@@ -65,11 +65,11 @@ public static void DirtyReadSecondThread(String connStrig, IsolationLevel level)
65
65
}
66
66
67
67
static class NonrepeatableReadThreads {
68
- public static void NonrepeatableReadFirstThread ( String connStrig , IsolationLevel level ) {
68
+ public static void NonrepeatableReadFirstThread ( string connStrig , IsolationLevel level ) {
69
69
Console . WriteLine ( "Begin the NonrepeatableReadFirstThread....." ) ;
70
70
71
71
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
72
- String cmdText = @"Use DbDataIsolationLevel;
72
+ string cmdText = @"Use DbDataIsolationLevel;
73
73
74
74
Select ProductId,ProductName,Quantity,Price
75
75
from dbo.Products
@@ -104,11 +104,11 @@ from dbo.Products
104
104
Console . WriteLine ( "Exit from the NonrepeatableReadFirstThread....." ) ;
105
105
}
106
106
107
- public static void NonrepeatableReadSecondThread ( String connStrig , IsolationLevel level ) {
107
+ public static void NonrepeatableReadSecondThread ( string connStrig , IsolationLevel level ) {
108
108
Console . WriteLine ( "Begin the NonrepeatableReadSecondThread....." ) ;
109
109
110
110
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
111
- String cmdText = @"Use DbDataIsolationLevel;
111
+ string cmdText = @"Use DbDataIsolationLevel;
112
112
113
113
WaitFor Delay '00:00:03';
114
114
@@ -130,11 +130,11 @@ public static void NonrepeatableReadSecondThread(String connStrig, IsolationLeve
130
130
}
131
131
132
132
static class PhantomReadThreads {
133
- public static void PhantomReadFirstThread ( String connStrig , IsolationLevel level ) {
133
+ public static void PhantomReadFirstThread ( string connStrig , IsolationLevel level ) {
134
134
Console . WriteLine ( "Begin the PhantomReadFirstThread....." ) ;
135
135
136
136
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
137
- String cmdText = @"Use DbDataIsolationLevel;
137
+ string cmdText = @"Use DbDataIsolationLevel;
138
138
139
139
Select ProductId,ProductName,Quantity,Price
140
140
from dbo.Products
@@ -168,11 +168,11 @@ from dbo.Products
168
168
Console . WriteLine ( "Exit from the PhantomReadFirstThread....." ) ;
169
169
}
170
170
171
- public static void PhantomReadSecondThread ( String connStrig , IsolationLevel level ) {
171
+ public static void PhantomReadSecondThread ( string connStrig , IsolationLevel level ) {
172
172
Console . WriteLine ( "Begin the PhantomReadSecondThread....." ) ;
173
173
174
174
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
175
- String cmdText = @"Use DbDataIsolationLevel;
175
+ string cmdText = @"Use DbDataIsolationLevel;
176
176
177
177
WaitFor Delay '00:00:03';
178
178
@@ -199,7 +199,7 @@ INSERT [dbo].[Products] ([ProductName], [Quantity], [Price])
199
199
// 2. Non-repeatable reads;
200
200
// 3. Phantoms.
201
201
static class TransactionIsolationLevels {
202
- public static void DemonstrateIsolationLevel ( String connString , IsolationLevel level ) {
202
+ public static void DemonstrateIsolationLevel ( string connString , IsolationLevel level ) {
203
203
// Before connect the database, recreate the table.
204
204
OperateDatabase . CreateTable ( connString ) ;
205
205
DemonstrateIsolationLevel ( connString , level , DirtyReadThreads . DirtyReadFirstThread , DirtyReadThreads . DirtyReadSecondThread ) ;
@@ -216,7 +216,7 @@ public static void DemonstrateIsolationLevel(String connString, IsolationLevel l
216
216
}
217
217
218
218
// Demonstrates if the specific transaction allows the specific behaviors.
219
- public static void DemonstrateIsolationLevel ( String connString , IsolationLevel level ,
219
+ public static void DemonstrateIsolationLevel ( string connString , IsolationLevel level ,
220
220
AsyncAccessDatabase firstThread , AsyncAccessDatabase secondThread ) {
221
221
Task [ ] tasks = {
222
222
Task . Factory . StartNew ( ( ) => firstThread ( connString , level ) ) ,
@@ -227,11 +227,11 @@ public static void DemonstrateIsolationLevel(String connString, IsolationLevel l
227
227
}
228
228
229
229
static class ExchangeValuesThreads {
230
- public static void ExchangeValuesFirstThread ( String connStrig , IsolationLevel level ) {
230
+ public static void ExchangeValuesFirstThread ( string connStrig , IsolationLevel level ) {
231
231
Console . WriteLine ( "Begin the ExchangeValuesFirstThread....." ) ;
232
232
233
233
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
234
- String cmdText = @"Use DbDataIsolationLevel;
234
+ string cmdText = @"Use DbDataIsolationLevel;
235
235
236
236
Declare @price money;
237
237
select @price=Price from dbo.Products where ProductId=2;
@@ -255,11 +255,11 @@ public static void ExchangeValuesFirstThread(String connStrig, IsolationLevel le
255
255
Console . WriteLine ( "Exit from the ExchangeValuesFirstThread....." ) ;
256
256
}
257
257
258
- public static void ExchangeValuesSecondThread ( String connStrig , IsolationLevel level ) {
258
+ public static void ExchangeValuesSecondThread ( string connStrig , IsolationLevel level ) {
259
259
Console . WriteLine ( "Begin the ExchangeValuesSecondThread....." ) ;
260
260
261
261
using ( SqlConnection conn = new SqlConnection ( connStrig ) ) {
262
- String cmdText = @"Use DbDataIsolationLevel;
262
+ string cmdText = @"Use DbDataIsolationLevel;
263
263
264
264
WaitFor Delay '00:00:03';
265
265
@@ -284,7 +284,7 @@ public static void ExchangeValuesSecondThread(String connStrig, IsolationLevel l
284
284
}
285
285
286
286
// Demonstrates the difference between the Serializable and Snapshot transaction
287
- public static void DemonstrateBetweenSnapshotAndSerializable ( String connString ) {
287
+ public static void DemonstrateBetweenSnapshotAndSerializable ( string connString ) {
288
288
OperateDatabase . CreateTable ( connString ) ;
289
289
290
290
Console . WriteLine ( "Exchange Vaules in the Snapshot transaction:" ) ;
@@ -302,9 +302,9 @@ public static void DemonstrateBetweenSnapshotAndSerializable(String connString)
302
302
DisplayData ( connString ) ;
303
303
}
304
304
305
- public static void DisplayData ( String connString ) {
305
+ public static void DisplayData ( string connString ) {
306
306
using ( SqlConnection conn = new SqlConnection ( connString ) ) {
307
- String cmdText = @"Use DbDataIsolationLevel;
307
+ string cmdText = @"Use DbDataIsolationLevel;
308
308
309
309
Select ProductId,ProductName,Quantity,Price
310
310
from dbo.Products" ;
@@ -340,9 +340,9 @@ public static void DisplayData(SqlDataReader reader) {
340
340
341
341
// This class includes database operations. If there's no database 'DbDataIsolationLevel', create the database.
342
342
static class OperateDatabase {
343
- public static Boolean CreateDatabase ( String connString ) {
343
+ public static Boolean CreateDatabase ( string connString ) {
344
344
using ( SqlConnection conn = new SqlConnection ( connString ) ) {
345
- String cmdText = @"Use Master;
345
+ string cmdText = @"Use Master;
346
346
347
347
if Db_Id('DbDataIsolationLevel') is null
348
348
create Database [DbDataIsolationLevel];" ;
@@ -359,9 +359,9 @@ public static Boolean CreateDatabase(String connString) {
359
359
}
360
360
361
361
// If there's no table [dbo].[Products] in DbDataIsolationLevel, create the table; or recreate it.
362
- public static Boolean CreateTable ( String connString ) {
362
+ public static Boolean CreateTable ( string connString ) {
363
363
using ( SqlConnection conn = new SqlConnection ( connString ) ) {
364
- String cmdText = @"Use DbDataIsolationLevel
364
+ string cmdText = @"Use DbDataIsolationLevel
365
365
366
366
if Object_ID('[dbo].[Products]') is not null
367
367
drop table [dbo].[Products]
@@ -384,9 +384,9 @@ [Price] money null
384
384
}
385
385
386
386
// Insert some rows into [dbo].[Products] table.
387
- public static Boolean InsertRows ( String connString ) {
387
+ public static Boolean InsertRows ( string connString ) {
388
388
using ( SqlConnection conn = new SqlConnection ( connString ) ) {
389
- String cmdText = @"Use DbDataIsolationLevel
389
+ string cmdText = @"Use DbDataIsolationLevel
390
390
391
391
INSERT [dbo].[Products] ([ProductName], [Quantity], [Price]) VALUES (N'Blue Bike', 365,1075.00)
392
392
INSERT [dbo].[Products] ([ProductName], [Quantity], [Price]) VALUES (N'Red Bike', 159, 1299.00)
@@ -401,9 +401,9 @@ INSERT [dbo].[Products] ([ProductName], [Quantity], [Price]) VALUES (N'Red Bike'
401
401
}
402
402
403
403
// Turn on or off 'ALLOW_SNAPSHOT_ISOLATION'
404
- public static Boolean SetSnapshot ( String connString , Boolean isOpen ) {
404
+ public static Boolean SetSnapshot ( string connString , Boolean isOpen ) {
405
405
using ( SqlConnection conn = new SqlConnection ( connString ) ) {
406
- String cmdText = null ;
406
+ string cmdText = null ;
407
407
408
408
if ( isOpen )
409
409
cmdText = @"ALTER DATABASE DbDataIsolationLevel SET ALLOW_SNAPSHOT_ISOLATION ON" ;
@@ -421,7 +421,7 @@ public static Boolean SetSnapshot(String connString, Boolean isOpen) {
421
421
}
422
422
class Program {
423
423
static void Main ( string [ ] args ) {
424
- String connString = "Data Source=(local);Initial Catalog=master;Integrated Security=True;Asynchronous Processing=true;" ;
424
+ string connString = "Data Source=(local);Initial Catalog=master;Integrated Security=True;Asynchronous Processing=true;" ;
425
425
426
426
OperateDatabase . CreateDatabase ( connString ) ;
427
427
Console . WriteLine ( ) ;
0 commit comments