File: SqlConnection.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (1751 lines) | stat: -rw-r--r-- 64,211 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
//
// System.Data.SqlClient.SqlConnection.cs
//
// Authors:
//   Rodrigo Moya (rodrigo@ximian.com)
//   Daniel Morgan (danmorg@sc.rr.com)
//   Tim Coleman (tim@timcoleman.com)
//   Phillip Jerkins (Phillip.Jerkins@morgankeegan.com)
//   Diego Caravana (diego@toth.it)
//
// Copyright (C) Ximian, Inc 2002
// Copyright (C) Daniel Morgan 2002, 2003
// Copyright (C) Tim Coleman, 2002, 2003
// Copyright (C) Phillip Jerkins, 2003
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using Mono.Data.Tds;
using Mono.Data.Tds.Protocol;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
#if !MONOTOUCH
using System.EnterpriseServices;
#endif // !MONOTOUCH
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Xml;
#if NET_2_0
using System.Collections.Generic;
#endif

namespace System.Data.SqlClient
{
	[DefaultEvent ("InfoMessage")]
#if NET_2_0
	public sealed class SqlConnection : DbConnection, IDbConnection, ICloneable
#else
	public sealed class SqlConnection : Component, IDbConnection, ICloneable
#endif // NET_2_0
	{
		#region Fields

		bool disposed;

		// The set of SQL connection pools
		static TdsConnectionPoolManager sqlConnectionPools = new TdsConnectionPoolManager (TdsVersion.tds80);
#if NET_2_0
		const int DEFAULT_PACKETSIZE = 8000;
		const int MAX_PACKETSIZE = 32768;
#else
		const int DEFAULT_PACKETSIZE = 8192;
		const int MAX_PACKETSIZE = 32767;
#endif
		const int MIN_PACKETSIZE = 512;
		const int DEFAULT_CONNECTIONTIMEOUT = 15;
		const int DEFAULT_MAXPOOLSIZE = 100;
		const int MIN_MAXPOOLSIZE = 1;
		const int DEFAULT_MINPOOLSIZE = 0;
		const int DEFAULT_PORT = 1433;

		// The current connection pool
		TdsConnectionPool pool;

		// The connection string that identifies this connection
		string connectionString;

		// The transaction object for the current transaction
		SqlTransaction transaction;

		// Connection parameters
		
		TdsConnectionParameters parms;
		bool connectionReset;
		bool pooling;
		string dataSource;
		int connectionTimeout;
		int minPoolSize;
		int maxPoolSize;
		int packetSize;
		int port;
		bool fireInfoMessageEventOnUserErrors;
		bool statisticsEnabled;
		
		// The current state
		ConnectionState state = ConnectionState.Closed;

		SqlDataReader dataReader;
		XmlReader xmlReader;

		// The TDS object
		Tds tds;

		#endregion // Fields

		#region Constructors

		public SqlConnection () : this (null)
		{
		}
	
		public SqlConnection (string connectionString)
		{
			ConnectionString = connectionString;
		}

		#endregion // Constructors

		#region Properties

#if NET_1_0 || ONLY_1_1
		[DataSysDescription ("Information used to connect to a DataSource, such as 'Data Source=x;Initial Catalog=x;Integrated Security=SSPI'.")]
#endif
		[DefaultValue ("")]
		[EditorAttribute ("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
		[RecommendedAsConfigurable (true)]
		[RefreshProperties (RefreshProperties.All)]
		public
#if NET_2_0
		override
#endif // NET_2_0
		string ConnectionString {
			get {
				if (connectionString == null)
					return string.Empty;
				return connectionString;
			}
			[MonoTODO("persist security info, encrypt, enlist keyword not implemented")]
			set {
				if (state == ConnectionState.Open)
					throw new InvalidOperationException ("Not Allowed to change ConnectionString property while Connection state is OPEN");
				SetConnectionString (value);
			}
		}
	
#if !NET_2_0
		[DataSysDescription ("Current connection timeout value, 'Connect Timeout=X' in the ConnectionString.")]	
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public
#if NET_2_0
		override
#endif // NET_2_0
		int ConnectionTimeout {
			get { return connectionTimeout; }
		}

#if !NET_2_0
		[DataSysDescription ("Current SQL Server database, 'Initial Catalog=X' in the connection string.")]
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public
#if NET_2_0
		override
#endif // NET_2_0
		string Database {
			get {
				if (State == ConnectionState.Open)
					return tds.Database; 
				return parms.Database ;
			}
		}
		
		internal SqlDataReader DataReader {
			get { return dataReader; }
			set { dataReader = value; }
		}

#if !NET_2_0
		[DataSysDescription ("Current SqlServer that the connection is opened to, 'Data Source=X' in the connection string. ")]
#else
		[Browsable(true)]
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public
#if NET_2_0
		override
#endif // NET_2_0
		string DataSource {
			get { return dataSource; }
		}

#if !NET_2_0
		[DataSysDescription ("Network packet size, 'Packet Size=x' in the connection string.")]
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public int PacketSize {
			get {
				if (State == ConnectionState.Open)
					return ((Tds) tds).PacketSize;
				return packetSize;
			}
		}

		[Browsable (false)]
#if !NET_2_0
		[DataSysDescription ("Version of the SQL Server accessed by the SqlConnection.")]
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public
#if NET_2_0
		override
#endif // NET_2_0
		string ServerVersion {
			get {
				if (state == ConnectionState.Closed)
					throw ExceptionHelper.ConnectionClosed ();
				else
					return tds.ServerVersion; 
			}
		}

		[Browsable (false)]
#if !NET_2_0
		[DataSysDescription ("The ConnectionState indicating whether the connection is open or closed.")]
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public
#if NET_2_0
		override
#endif // NET_2_0
		ConnectionState State {
			get { return state; }
		}

		internal Tds Tds {
			get { return tds; }
		}

		internal SqlTransaction Transaction {
			get { return transaction; }
			set { transaction = value; }
		}

#if !NET_2_0
		[DataSysDescription ("Workstation Id, 'Workstation ID=x' in the connection string.")]
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public string WorkstationId {
			get { return parms.Hostname; }
		}

		internal XmlReader XmlReader {
			get { return xmlReader; }
			set { xmlReader = value; }
		}

#if NET_2_0
		public bool FireInfoMessageEventOnUserErrors { 
			get { return fireInfoMessageEventOnUserErrors; } 
			set { fireInfoMessageEventOnUserErrors = value; }
		}
		
		[DefaultValue (false)]
		public bool StatisticsEnabled { 
			get { return statisticsEnabled; } 
			set { statisticsEnabled = value; }
		}
#endif
		#endregion // Properties

		#region Events

#if !NET_2_0
		[DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
#endif
		public event SqlInfoMessageEventHandler InfoMessage;

#if !NET_2_0
		[DataSysDescription ("Event triggered when the connection changes state.")]
		public new event StateChangeEventHandler StateChange;
#endif

		#endregion // Events

		#region Delegates

		private void ErrorHandler (object sender, TdsInternalErrorMessageEventArgs e)
		{
			try {
				if (!tds.IsConnected)
					Close ();
			} catch {
				try {
					Close ();
				} catch {
				}
			}
			throw new SqlException (e.Class, e.LineNumber, e.Message, e.Number, e.Procedure, e.Server, "Mono SqlClient Data Provider", e.State);
		}

		private void MessageHandler (object sender, TdsInternalInfoMessageEventArgs e)
		{
			OnSqlInfoMessage (CreateSqlInfoMessageEvent (e.Errors));
		}

		#endregion // Delegates

		#region Methods

		public new SqlTransaction BeginTransaction ()
		{
			return BeginTransaction (IsolationLevel.ReadCommitted, String.Empty);
		}

		public new SqlTransaction BeginTransaction (IsolationLevel iso)
		{
			return BeginTransaction (iso, String.Empty);
		}

		public SqlTransaction BeginTransaction (string transactionName)
		{
			return BeginTransaction (IsolationLevel.ReadCommitted, transactionName);
		}

		public SqlTransaction BeginTransaction (IsolationLevel iso, string transactionName)
		{
			if (state == ConnectionState.Closed)
				throw ExceptionHelper.ConnectionClosed ();
			if (transaction != null)
				throw new InvalidOperationException ("SqlConnection does not support parallel transactions.");

			string isolevel = String.Empty;
			switch (iso) {
			case IsolationLevel.ReadUncommitted:
				isolevel = "READ UNCOMMITTED";
				break;
			case IsolationLevel.RepeatableRead:
				isolevel = "REPEATABLE READ";
				break;
			case IsolationLevel.Serializable:
				isolevel = "SERIALIZABLE";
				break;
			case IsolationLevel.ReadCommitted:
				isolevel = "READ COMMITTED";
				break;
#if NET_2_0
			case IsolationLevel.Snapshot:
				isolevel = "SNAPSHOT";
				break;
			case IsolationLevel.Unspecified:
				iso = IsolationLevel.ReadCommitted;
				isolevel = "READ COMMITTED";
				break;
			case IsolationLevel.Chaos:
				throw new ArgumentOutOfRangeException ("IsolationLevel",
					string.Format (CultureInfo.CurrentCulture,
						"The IsolationLevel enumeration " +
						"value, {0}, is not supported by " +
						"the .Net Framework SqlClient " +
						"Data Provider.", (int) iso));
#endif
			default:
#if NET_2_0
				throw new ArgumentOutOfRangeException ("IsolationLevel",
					string.Format (CultureInfo.CurrentCulture,
						"The IsolationLevel enumeration value, {0}, is invalid.",
						(int) iso));
#else
				throw new ArgumentException ("Invalid IsolationLevel parameter: must be ReadCommitted, ReadUncommitted, RepeatableRead, or Serializable.");
#endif
			}

			tds.Execute (String.Format ("SET TRANSACTION ISOLATION LEVEL {0};BEGIN TRANSACTION {1}", isolevel, transactionName));
			
			transaction = new SqlTransaction (this, iso);
			return transaction;
		}

		public
#if NET_2_0
		override
#endif // NET_2_0
		void ChangeDatabase (string database)
		{
			if (!IsValidDatabaseName (database))
				throw new ArgumentException (String.Format ("The database name {0} is not valid.", database));
			if (state != ConnectionState.Open)
				throw new InvalidOperationException ("The connection is not open.");
			tds.Execute (String.Format ("use [{0}]", database));
		}

		private void ChangeState (ConnectionState currentState)
		{
			if (currentState == state)
				return;

			ConnectionState originalState = state;
			state = currentState;
			OnStateChange (CreateStateChangeEvent (originalState, currentState));
		}

		public
#if NET_2_0
		override
#endif // NET_2_0
		void Close ()
		{
			if (transaction != null && transaction.IsOpen)
				transaction.Rollback ();

			if (dataReader != null || xmlReader != null) {
				if(tds != null) tds.SkipToEnd ();
				dataReader = null;
				xmlReader = null;
			}

			if (tds != null && tds.IsConnected) {
				if (pooling && tds.Pooling) {
					if (pool != null) {
						pool.ReleaseConnection (tds);
						pool = null;
					}
				} else {
					tds.Disconnect ();
				}
			}

			if (tds != null) {
				tds.TdsErrorMessage -= new TdsInternalErrorMessageEventHandler (ErrorHandler);
				tds.TdsInfoMessage -= new TdsInternalInfoMessageEventHandler (MessageHandler);
			}

			ChangeState (ConnectionState.Closed);
		}

		public new SqlCommand CreateCommand ()
		{
			SqlCommand command = new SqlCommand ();
			command.Connection = this;
			return command;
		}
		
		private SqlInfoMessageEventArgs CreateSqlInfoMessageEvent (TdsInternalErrorCollection errors)
		{
			return new SqlInfoMessageEventArgs (errors);
		}

		private StateChangeEventArgs CreateStateChangeEvent (ConnectionState originalState, ConnectionState currentState)
		{
			return new StateChangeEventArgs (originalState, currentState);
		}

		protected override void Dispose (bool disposing)
		{
			try {
				if (disposing && !disposed) {
					if (State == ConnectionState.Open) 
						Close ();
					ConnectionString = null;
				}
			} finally {
				disposed = true;
				base.Dispose (disposing);
			}
		}

#if !MONOTOUCH
		[MonoTODO ("Not sure what this means at present.")]
		public void EnlistDistributedTransaction (ITransaction transaction)
		{
			throw new NotImplementedException ();
		}
#endif // !MONOTOUCH

		object ICloneable.Clone ()
		{
			return new SqlConnection (ConnectionString);
		}

#if NET_2_0
		protected override DbTransaction BeginDbTransaction (IsolationLevel isolationLevel)
		{
			return BeginTransaction (isolationLevel);
		}

		protected override DbCommand CreateDbCommand ()
		{
			return CreateCommand ();
		}
#else
		IDbTransaction IDbConnection.BeginTransaction ()
		{
			return BeginTransaction ();
		}

		IDbTransaction IDbConnection.BeginTransaction (IsolationLevel iso)
		{
			return BeginTransaction (iso);
		}

		IDbCommand IDbConnection.CreateCommand ()
		{
			return CreateCommand ();
		}
#endif

		public
#if NET_2_0
		override
#endif // NET_2_0
		void Open ()
		{
			string serverName = string.Empty;
			if (state == ConnectionState.Open)
				throw new InvalidOperationException ("The Connection is already Open (State=Open)");

			if (connectionString == null || connectionString.Trim().Length == 0)
				throw new InvalidOperationException ("Connection string has not been initialized.");

			try {
				if (!pooling) {
					if(!ParseDataSource (dataSource, out port, out serverName))
						throw new SqlException(20, 0, "SQL Server does not exist or access denied.",  17, "ConnectionOpen (Connect()).", dataSource, parms.ApplicationName, 0);
					tds = new Tds80 (serverName, port, PacketSize, ConnectionTimeout);
					tds.Pooling = false;
				}
				else {
					if(!ParseDataSource (dataSource, out port, out serverName))
						throw new SqlException(20, 0, "SQL Server does not exist or access denied.",  17, "ConnectionOpen (Connect()).", dataSource, parms.ApplicationName, 0);
					
 					TdsConnectionInfo info = new TdsConnectionInfo (serverName, port, packetSize, ConnectionTimeout, minPoolSize, maxPoolSize);
					pool = sqlConnectionPools.GetConnectionPool (connectionString, info);
					tds = pool.GetConnection ();
				}
			} catch (TdsTimeoutException e) {
				throw SqlException.FromTdsInternalException ((TdsInternalException) e);
			} catch (TdsInternalException e) {
				throw SqlException.FromTdsInternalException (e);
			}

			tds.TdsErrorMessage += new TdsInternalErrorMessageEventHandler (ErrorHandler);
			tds.TdsInfoMessage += new TdsInternalInfoMessageEventHandler (MessageHandler);

			if (!tds.IsConnected) {
				try {
					tds.Connect (parms);
				} catch {
					if (pooling)
						pool.ReleaseConnection (tds);
					throw;
				}
			}

			disposed = false; // reset this, so using () would call Close ().
			ChangeState (ConnectionState.Open);
		}

		private bool ParseDataSource (string theDataSource, out int thePort, out string theServerName)
		{
			theServerName = string.Empty;
			string theInstanceName = string.Empty;
	
			if (theDataSource == null)
				throw new ArgumentException("Format of initialization string does not conform to specifications");

			thePort = DEFAULT_PORT; // default TCP port for SQL Server
			bool success = true;

			int idx = 0;
			if ((idx = theDataSource.IndexOf (',')) > -1) {
				theServerName = theDataSource.Substring (0, idx);
				string p = theDataSource.Substring (idx + 1);
				thePort = Int32.Parse (p);
			} else if ((idx = theDataSource.IndexOf ('\\')) > -1) {
				theServerName = theDataSource.Substring (0, idx);
				theInstanceName = theDataSource.Substring (idx + 1);

				// do port discovery via UDP port 1434
				port = DiscoverTcpPortViaSqlMonitor (theServerName, theInstanceName);
				if (port == -1)
					success = false;
			} else
				theServerName = theDataSource;

			if (theServerName.Length == 0 || theServerName == "(local)" || theServerName == ".")
				theServerName = "localhost";

			if ((idx = theServerName.IndexOf ("tcp:")) > -1)
				theServerName = theServerName.Substring (idx + 4);

			return success;
		}

		private bool ConvertIntegratedSecurity (string value)
		{
			if (value.ToUpper() == "SSPI")
				return true;

			return ConvertToBoolean ("integrated security", value, false);
		}

		private bool ConvertToBoolean (string key, string value, bool defaultValue)
		{
			if (value.Length == 0)
				return defaultValue;

			string upperValue = value.ToUpper ();

			if (upperValue == "TRUE" || upperValue == "YES")
				return true;
			else if (upperValue == "FALSE" || upperValue == "NO")
				return false;

			throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
				"Invalid value \"{0}\" for key '{1}'.", value, key));
		}

		private int ConvertToInt32 (string key, string value, int defaultValue)
		{
			if (value.Length == 0)
				return defaultValue;

			try {
				return int.Parse (value);
			} catch (Exception ex) {
				throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
					"Invalid value \"{0}\" for key '{1}'.", value, key), ex);
			}
		}

		private int DiscoverTcpPortViaSqlMonitor (string ServerName, string InstanceName) 
		{
			SqlMonitorSocket msock;
			msock = new SqlMonitorSocket (ServerName, InstanceName);
			int SqlServerPort = msock.DiscoverTcpPort (ConnectionTimeout);
			msock = null;
			return SqlServerPort;
		}
	
		void SetConnectionString (string connectionString)
		{
			SetDefaultConnectionParameters ();

			if ((connectionString == null) || (connectionString.Trim().Length == 0)) {
				this.connectionString = connectionString;
				return;
			}

			connectionString += ";";

			bool inQuote = false;
			bool inDQuote = false;
			bool inName = true;

			string name = String.Empty;
			string value = String.Empty;
			StringBuilder sb = new StringBuilder ();

			for (int i = 0; i < connectionString.Length; i += 1) {
				char c = connectionString [i];
				char peek;
				if (i == connectionString.Length - 1)
					peek = '\0';
				else
					peek = connectionString [i + 1];

				switch (c) {
				case '\'':
					if (inDQuote)
						sb.Append (c);
					else if (peek.Equals (c)) {
						sb.Append (c);
						i += 1;
					}
					else
						inQuote = !inQuote;
					break;
				case '"':
					if (inQuote)
						sb.Append (c);
					else if (peek.Equals (c)) {
						sb.Append (c);
						i += 1;
					}
					else
						inDQuote = !inDQuote;
					break;
				case ';':
					if (inDQuote || inQuote)
						sb.Append (c);
					else {
						if (name != String.Empty && name != null) {
							value = sb.ToString ();
							SetProperties (name.ToLower ().Trim() , value);
						}
						else if (sb.Length != 0)
							throw new ArgumentException ("Format of initialization string does not conform to specifications");
						inName = true;
						name = String.Empty;
						value = String.Empty;
						sb = new StringBuilder ();
					}
					break;
				case '=':
					if (inDQuote || inQuote || !inName)
						sb.Append (c);
					else if (peek.Equals (c)) {
						sb.Append (c);
						i += 1;

					}
					else {
						name = sb.ToString ();
						sb = new StringBuilder ();
						inName = false;
					}
					break;
				case ' ':
					if (inQuote || inDQuote)
						sb.Append (c);
					else if (sb.Length > 0 && !peek.Equals (';'))
						sb.Append (c);
					break;
				default:
					sb.Append (c);
					break;
				}
			}

			if (minPoolSize > maxPoolSize)
				throw new ArgumentException ("Invalid value for "
					+ "'min pool size' or 'max pool size'; "
					+ "'min pool size' must not be greater "
					+ "than 'max pool size'.");

			connectionString = connectionString.Substring (0 , connectionString.Length-1);
			this.connectionString = connectionString;
		}

		void SetDefaultConnectionParameters ()
		{
			if (parms == null)
				parms = new TdsConnectionParameters ();
			else
				parms.Reset ();
			dataSource = string.Empty;
			connectionTimeout = DEFAULT_CONNECTIONTIMEOUT;
			connectionReset = true;
			pooling = true;
			maxPoolSize = DEFAULT_MAXPOOLSIZE;
			minPoolSize = DEFAULT_MINPOOLSIZE;
			packetSize = DEFAULT_PACKETSIZE;
			port = DEFAULT_PORT;
 #if NET_2_0
			async = false;
 #endif
		}

		private void SetProperties (string name , string value)
		{
			switch (name) {
			case "app" :
			case "application name" :
				parms.ApplicationName = value;
				break;
			case "attachdbfilename" :
			case "extended properties" :
			case "initial file name" :
				parms.AttachDBFileName = value;
				break;
			case "timeout" :
			case "connect timeout" :
			case "connection timeout" :
				int tmpTimeout = ConvertToInt32 ("connect timeout", value,
					DEFAULT_CONNECTIONTIMEOUT);
				if (tmpTimeout < 0)
					throw new ArgumentException ("Invalid 'connect timeout'. Must be an integer >=0 ");
				else 
					connectionTimeout = tmpTimeout;
				break;
			case "connection lifetime" :
				break;
			case "connection reset" :
				connectionReset = ConvertToBoolean ("connection reset", value, true);
				break;
			case "language" :
			case "current language" :
				parms.Language = value;
				break;
			case "data source" :
			case "server" :
			case "address" :
			case "addr" :
			case "network address" :
				dataSource = value;
				break;
			case "encrypt":
				if (ConvertToBoolean (name, value, false))
					throw new NotImplementedException("SSL encryption for"
						+ " data sent between client and server is not"
						+ " implemented.");
				break;
			case "enlist" :
				if (!ConvertToBoolean (name, value, true))
					throw new NotImplementedException("Disabling the automatic"
						+ " enlistment of connections in the thread's current"
						+ " transaction context is not implemented.");
				break;
			case "initial catalog" :
			case "database" :
				parms.Database = value;
				break;
			case "integrated security" :
			case "trusted_connection" :
				parms.DomainLogin = ConvertIntegratedSecurity(value);
				break;
			case "max pool size" :
				int tmpMaxPoolSize = ConvertToInt32 (name, value, DEFAULT_MAXPOOLSIZE);
				if (tmpMaxPoolSize < MIN_MAXPOOLSIZE)
					throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
						"Invalid '{0}'. The value must be greater than {1}.",
						name, MIN_MAXPOOLSIZE));
				else
					maxPoolSize = tmpMaxPoolSize;
				break;
			case "min pool size" :
				int tmpMinPoolSize = ConvertToInt32 (name, value, DEFAULT_MINPOOLSIZE);
				if (tmpMinPoolSize < 0)
					throw new ArgumentException ("Invalid 'min pool size'. Must be a integer >= 0");
				else
					minPoolSize = tmpMinPoolSize;
				break;
#if NET_2_0
			case "multipleactiveresultsets":
				// FIXME: not implemented
				ConvertToBoolean (name, value, false);
				break;
			case "asynchronous processing" :
			case "async" :
				async = ConvertToBoolean (name, value, false);
				break;
#endif
			case "net" :
			case "network" :
			case "network library" :
				if (!value.ToUpper ().Equals ("DBMSSOCN"))
					throw new ArgumentException ("Unsupported network library.");
				break;
			case "packet size" :
				int tmpPacketSize = ConvertToInt32 (name, value, DEFAULT_PACKETSIZE);
				if (tmpPacketSize < MIN_PACKETSIZE || tmpPacketSize > MAX_PACKETSIZE)
					throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
						"Invalid 'Packet Size'. The value must be between {0} and {1}.",
						MIN_PACKETSIZE, MAX_PACKETSIZE));
				else
					packetSize = tmpPacketSize;
				break;
			case "password" :
			case "pwd" :
				parms.Password = value;
				break;
			case "persistsecurityinfo" :
			case "persist security info" :
				// FIXME : not implemented
				// throw new NotImplementedException ();
				break;
			case "pooling" :
				pooling = ConvertToBoolean (name, value, true);
				break;
			case "uid" :
			case "user" :
			case "user id" :
				parms.User = value;
				break;
			case "wsid" :
			case "workstation id" :
				parms.Hostname = value;
				break;
#if NET_2_0
			case "user instance":
				userInstance = ConvertToBoolean (name, value, false);
				break;
#endif
			default :
				throw new ArgumentException("Keyword not supported : '" + name + "'.");
			}
		}

		static bool IsValidDatabaseName (string database)
		{
			if ( database == null || database.Trim().Length == 0 || database.Length > 128)
				return false ;
			
			if (database[0] == '"' && database[database.Length] == '"')
				database = database.Substring (1, database.Length - 2);
			else if (Char.IsDigit (database[0]))
				return false;

			if (database[0] == '_')
				return false;

			foreach (char c  in database.Substring (1, database.Length - 1))
				if (!Char.IsLetterOrDigit (c) && c != '_' && c != '-')
					return false;
			return true;
		}

		private void OnSqlInfoMessage (SqlInfoMessageEventArgs value)
		{
			if (InfoMessage != null)
				InfoMessage (this, value);
		}

#if !NET_2_0
		private new void OnStateChange (StateChangeEventArgs value)
		{
			if (StateChange != null)
				StateChange (this, value);
		}
#endif

		private sealed class SqlMonitorSocket : UdpClient
		{
			// UDP port that the SQL Monitor listens
			private static readonly int SqlMonitorUdpPort = 1434;
			//private static readonly string SqlServerNotExist = "SQL Server does not exist or access denied";

			private string server;
			private string instance;

			internal SqlMonitorSocket (string ServerName, string InstanceName) 
				: base (ServerName, SqlMonitorUdpPort) 
			{
				server = ServerName;
				instance = InstanceName;
			}

			internal int DiscoverTcpPort (int timeoutSeconds)
			{
				int SqlServerTcpPort;
				Client.Blocking = false;
				// send command to UDP 1434 (SQL Monitor) to get
				// the TCP port to connect to the MS SQL server
				ASCIIEncoding enc = new ASCIIEncoding ();
				Byte[] rawrq = new Byte [instance.Length + 1];
				rawrq[0] = 4;
				enc.GetBytes (instance, 0, instance.Length, rawrq, 1);
				Send (rawrq, rawrq.Length);

				if (!Active)
					return -1; // Error
				
				bool result;
				long timeout = timeoutSeconds * 1000000;
				result = Client.Poll ((int)timeout, SelectMode.SelectRead);
				if (result == false)
					return -1; // Error

				if (Client.Available <= 0)
					return -1; // Error
#if NET_2_0
				IPEndPoint endpoint = new IPEndPoint (Dns.GetHostEntry ("localhost").AddressList [0], 0);
#else
				IPEndPoint endpoint = new IPEndPoint (Dns.GetHostByName ("localhost").AddressList [0], 0);
#endif
				Byte [] rawrs;

				rawrs = Receive (ref endpoint);

				string rs = Encoding.ASCII.GetString (rawrs);

				string[] rawtokens = rs.Split (';');
				Hashtable data = new Hashtable ();
				for (int i = 0; i < rawtokens.Length / 2 && i < 256; i++) {
					data [rawtokens [i * 2]] = rawtokens [ i * 2 + 1];
				}

				if (!data.ContainsKey ("tcp")) {
					string msg = "Mono does not support names pipes or shared memory "
						+ "for connecting to SQL Server. Please enable the TCP/IP "
						+ "protocol.";
					throw new NotImplementedException (msg);
				}

				SqlServerTcpPort = int.Parse ((string) data ["tcp"]);
				Close ();

				return SqlServerTcpPort;
			}
		}

#if NET_2_0
		struct ColumnInfo
		{
			public string name;
			public Type type;

			public ColumnInfo (string name, Type type)
			{
				this.name = name; this.type = type;
			}
		}

		static class ReservedWords
		{
			static readonly string [] reservedWords =
			{
				"ADD", "EXCEPT", "PERCENT", "ALL", "EXEC", "PLAN", "ALTER",
				  "EXECUTE", "PRECISION", "AND", "EXISTS", "PRIMARY", "ANY",
				  "EXIT", "PRINT", "AS", "FETCH", "PROC", "ASC", "FILE",
				  "PROCEDURE", "AUTHORIZATION", "FILLFACTOR", "PUBLIC",
				  "BACKUP", "FOR", "RAISERROR", "BEGIN", "FOREIGN", "READ",
				  "BETWEEN", "FREETEXT", "READTEXT", "BREAK", "FREETEXTTABLE",
				  "RECONFIGURE", "BROWSE", "FROM", "REFERENCES", "BULK",
				  "FULL", "REPLICATION", "BY", "FUNCTION", "RESTORE",
				  "CASCADE", "GOTO", "RESTRICT", "CASE", "GRANT", "RETURN",
				  "CHECK", "GROUP", "REVOKE", "CHECKPOINT", "HAVING", "RIGHT",
				  "CLOSE", "HOLDLOCK", "ROLLBACK", "CLUSTERED", "IDENTITY",
				  "ROWCOUNT", "COALESCE", "IDENTITY_INSERT", "ROWGUIDCOL",
				  "COLLATE", "IDENTITYCOL", "RULE", "COLUMN", "IF", "SAVE",
				  "COMMIT", "IN", "SCHEMA", "COMPUTE", "INDEX", "SELECT",
				  "CONSTRAINT", "INNER", "SESSION_USER", "CONTAINS", "INSERT",
				  "SET", "CONTAINSTABLE", "INTERSECT", "SETUSER", "CONTINUE",
				  "INTO", "SHUTDOWN", "CONVERT", "IS", "SOME", "CREATE",
				  "JOIN", "STATISTICS", "CROSS", "KEY", "SYSTEM_USER",
				  "CURRENT", "KILL", "TABLE", "CURRENT_DATE", "LEFT",
				  "TEXTSIZE", "CURRENT_TIME", "LIKE", "THEN",
				  "CURRENT_TIMESTAMP", "LINENO", "TO", "CURRENT_USER", "LOAD",
				  "TOP", "CURSOR", "NATIONAL", "TRAN", "DATABASE", "NOCHECK",
				  "TRANSACTION", "DBCC", "NONCLUSTERED", "TRIGGER",
				  "DEALLOCATE", "NOT", "TRUNCATE", "DECLARE", "NULL",
				  "TSEQUAL", "DEFAULT", "NULLIF", "UNION", "DELETE", "OF",
				  "UNIQUE", "DENY", "OFF", "UPDATE", "DESC", "OFFSETS",
				  "UPDATETEXT", "DISK", "ON", "USE", "DISTINCT", "OPEN",
				  "USER", "DISTRIBUTED", "OPENDATASOURCE", "VALUES", "DOUBLE",
				  "OPENQUERY", "VARYING", "DROP", "OPENROWSET", "VIEW",
				  "DUMMY", "OPENXML", "WAITFOR", "DUMP", "OPTION", "WHEN",
				  "ELSE", "OR", "WHERE", "END", "ORDER", "WHILE", "ERRLVL",
				  "OUTER", "WITH", "ESCAPE", "OVER", "WRITETEXT", "ABSOLUTE",
				  "FOUND", "PRESERVE", "ACTION", "FREE", "PRIOR", "ADMIN",
				  "GENERAL", "PRIVILEGES", "AFTER", "GET", "READS",
				  "AGGREGATE", "GLOBAL", "REAL", "ALIAS", "GO", "RECURSIVE",
				  "ALLOCATE", "GROUPING", "REF", "ARE", "HOST", "REFERENCING",
				  "ARRAY", "HOUR", "RELATIVE", "ASSERTION", "IGNORE", "RESULT",
				  "AT", "IMMEDIATE", "RETURNS", "BEFORE", "INDICATOR", "ROLE",
				  "BINARY", "INITIALIZE", "ROLLUP", "BIT", "INITIALLY",
				  "ROUTINE", "BLOB", "INOUT", "ROW", "BOOLEAN", "INPUT",
				  "ROWS", "BOTH", "INT", "SAVEPOINT", "BREADTH", "INTEGER",
				  "SCROLL", "CALL", "INTERVAL", "SCOPE", "CASCADED",
				  "ISOLATION", "SEARCH", "CAST", "ITERATE", "SECOND",
				  "CATALOG", "LANGUAGE", "SECTION", "CHAR", "LARGE",
				  "SEQUENCE", "CHARACTER", "LAST", "SESSION", "CLASS",
				  "LATERAL", "SETS", "CLOB", "LEADING", "SIZE", "COLLATION",
				  "LESS", "SMALLINT", "COMPLETION", "LEVEL", "SPACE",
				  "CONNECT", "LIMIT", "SPECIFIC", "CONNECTION", "LOCAL",
				  "SPECIFICTYPE", "CONSTRAINTS", "LOCALTIME", "SQL",
				  "CONSTRUCTOR", "LOCALTIMESTAMP", "SQLEXCEPTION",
				  "CORRESPONDING", "LOCATOR", "SQLSTATE", "CUBE", "MAP",
				  "SQLWARNING", "CURRENT_PATH", "MATCH", "START",
				  "CURRENT_ROLE", "MINUTE", "STATE", "CYCLE", "MODIFIES",
				  "STATEMENT", "DATA", "MODIFY", "STATIC", "DATE", "MODULE",
				  "STRUCTURE", "DAY", "MONTH", "TEMPORARY", "DEC", "NAMES",
				  "TERMINATE", "DECIMAL", "NATURAL", "THAN", "DEFERRABLE",
				  "NCHAR", "TIME", "DEFERRED", "NCLOB", "TIMESTAMP", "DEPTH",
				  "NEW", "TIMEZONE_HOUR", "DEREF", "NEXT", "TIMEZONE_MINUTE",
				  "DESCRIBE", "NO", "TRAILING", "DESCRIPTOR", "NONE",
				  "TRANSLATION", "DESTROY", "NUMERIC", "TREAT", "DESTRUCTOR",
				  "OBJECT", "TRUE", "DETERMINISTIC", "OLD", "UNDER",
				  "DICTIONARY", "ONLY", "UNKNOWN", "DIAGNOSTICS", "OPERATION",
				  "UNNEST", "DISCONNECT", "ORDINALITY", "USAGE", "DOMAIN",
				  "OUT", "USING", "DYNAMIC", "OUTPUT", "VALUE", "EACH",
				  "PAD", "VARCHAR", "END-EXEC", "PARAMETER", "VARIABLE",
				  "EQUALS", "PARAMETERS", "WHENEVER", "EVERY", "PARTIAL",
				  "WITHOUT", "EXCEPTION", "PATH", "WORK", "EXTERNAL",
				  "POSTFIX", "WRITE", "FALSE", "PREFIX", "YEAR", "FIRST",
				  "PREORDER", "ZONE", "FLOAT", "PREPARE", "ADA", "AVG",
				  "BIT_LENGTH", "CHAR_LENGTH", "CHARACTER_LENGTH", "COUNT",
				  "EXTRACT", "FORTRAN", "INCLUDE", "INSENSITIVE", "LOWER",
				  "MAX", "MIN", "OCTET_LENGTH", "OVERLAPS", "PASCAL",
				  "POSITION", "SQLCA", "SQLCODE", "SQLERROR", "SUBSTRING",
				  "SUM", "TRANSLATE", "TRIM", "UPPER"
			};
			static DataTable instance;
			static public DataTable Instance {
				get {
					if (instance == null) {
						DataRow row = null;
						instance = new DataTable ("ReservedWords");
						instance.Columns.Add ("ReservedWord", typeof(string));
						foreach (string reservedWord in reservedWords)
						{
							row = instance.NewRow();

							row["ReservedWord"] = reservedWord;
							instance.Rows.Add(row);
						}
					}
					return instance;
				}
			}
		}

		static class MetaDataCollections
		{
			static readonly ColumnInfo [] columns = {
				new ColumnInfo ("CollectionName", typeof (string)),
				new ColumnInfo ("NumberOfRestrictions", typeof (int)),
				new ColumnInfo ("NumberOfIdentifierParts", typeof (int))
			};

			static readonly object [][] rows = {
				new object [] {"MetaDataCollections", 0, 0},
				new object [] {"DataSourceInformation", 0, 0},
				new object [] {"DataTypes", 0, 0},
				new object [] {"Restrictions", 0, 0},
				new object [] {"ReservedWords", 0, 0},
				new object [] {"Users", 1, 1},
				new object [] {"Databases", 1, 1},
				new object [] {"Tables", 4, 3},
				new object [] {"Columns", 4, 4},
				new object [] {"StructuredTypeMembers", 4, 4},
				new object [] {"Views", 3, 3},
				new object [] {"ViewColumns", 4, 4},
				new object [] {"ProcedureParameters", 4, 1},
				new object [] {"Procedures", 4, 3},
				new object [] {"ForeignKeys", 4, 3},
				new object [] {"IndexColumns", 5, 4},
				new object [] {"Indexes", 4, 3},
				new object [] {"UserDefinedTypes", 2, 1}
			};

			static DataTable instance;
			static public DataTable Instance {
				get {
					if (instance == null) {
						instance = new DataTable ("MetaDataCollections");
						foreach (ColumnInfo c in columns)
							instance.Columns.Add (c.name, c.type);
						foreach (object [] row in rows)
							instance.LoadDataRow (row, true);
					}
					return instance;
				}
			}
		}

		static class DataSourceInformation
		{
			static readonly ColumnInfo [] columns = {
				new ColumnInfo ("CompositeIdentifierSeparatorPattern", typeof (string)),
				new ColumnInfo ("DataSourceProductName", typeof(string)),
				new ColumnInfo ("DataSourceProductVersion", typeof(string)),
				new ColumnInfo ("DataSourceProductVersionNormalized", typeof(string)),
				new ColumnInfo ("GroupByBehavior", typeof(GroupByBehavior)),
				new ColumnInfo ("IdentifierPattern", typeof(string)),
				new ColumnInfo ("IdentifierCase", typeof(IdentifierCase)),
				new ColumnInfo ("OrderByColumnsInSelect", typeof(bool)),
				new ColumnInfo ("ParameterMarkerFormat", typeof(string)),
				new ColumnInfo ("ParameterMarkerPattern", typeof(string)),
				new ColumnInfo ("ParameterNameMaxLength", typeof(int)),
				new ColumnInfo ("ParameterNamePattern", typeof(string)),
				new ColumnInfo ("QuotedIdentifierPattern", typeof(string)),
				new ColumnInfo ("QuotedIdentifierCase", typeof(IdentifierCase)),
				new ColumnInfo ("StatementSeparatorPattern", typeof(string)),
				new ColumnInfo ("StringLiteralPattern", typeof(string)),
				new ColumnInfo ("SupportedJoinOperators", typeof(SupportedJoinOperators))
			};

			static public DataTable GetInstance (SqlConnection conn)
			{
				DataTable table = new DataTable ("DataSourceInformation");
				foreach (ColumnInfo c in columns)
					table.Columns.Add (c.name, c.type);
				DataRow row = table.NewRow ();
				row [0] = "\\.";
				row [1] = "Microsoft SQL Server";
				row [2] = conn.ServerVersion;;
				row [3] = conn.ServerVersion;;
				row [4] = GroupByBehavior.Unrelated;
				row [5] = @"(^\[\p{Lo}\p{Lu}\p{Ll}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Nd}@$#_]*$)|(^\[[^\]\0]|\]\]+\]$)|(^\""[^\""\0]|\""\""+\""$)";
				row [6] = IdentifierCase.Insensitive; // FIXME: obtain this from SQL Server
				row [7] = false;
				row [8] = "{0}";
				row [9] = @"@[\p{Lo}\p{Lu}\p{Ll}\p{Lm}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Lm}\p{Nd}\uff3f_@#\$]*(?=\s+|$)";
				row [10] = 128;
				row [11] = @"^[\p{Lo}\p{Lu}\p{Ll}\p{Lm}_@#][\p{Lo}\p{Lu}\p{Ll}\p{Lm}\p{Nd}\uff3f_@#\$]*(?=\s+|$)";
				row [12] = @"(([^\[]|\]\])*)";
				row [13] = IdentifierCase.Insensitive;  // FIXME: obtain this from SQL Server
				row [14] =";";
				row [15] = "'(([^']|'')*)'";
				row [16] = (SupportedJoinOperators.FullOuter | SupportedJoinOperators.Inner |
					SupportedJoinOperators.LeftOuter | SupportedJoinOperators.RightOuter);
				table.Rows.Add (row);
				return table;
			}
		}

		static class DataTypes
		{
			static readonly ColumnInfo [] columns = {
				new ColumnInfo ("TypeName", typeof(string)),
				new ColumnInfo ("ProviderDbType", typeof(int)),
				new ColumnInfo ("ColumnSize", typeof(long)),
				new ColumnInfo ("CreateFormat", typeof(string)),
				new ColumnInfo ("CreateParameters", typeof(string)),
				new ColumnInfo ("DataType", typeof(string)),
				new ColumnInfo ("IsAutoIncrementable", typeof(bool)),
				new ColumnInfo ("IsBestMatch", typeof(bool)),
				new ColumnInfo ("IsCaseSensitive", typeof(bool)),
				new ColumnInfo ("IsFixedLength", typeof(bool)),
				new ColumnInfo ("IsFixedPrecisionScale", typeof(bool)),
				new ColumnInfo ("IsLong", typeof(bool)),
				new ColumnInfo ("IsNullable", typeof(bool)),
				new ColumnInfo ("IsSearchable", typeof(bool)),
				new ColumnInfo ("IsSearchableWithLike", typeof(bool)),
				new ColumnInfo ("IsUnsigned", typeof(bool)),
				new ColumnInfo ("MaximumScale", typeof(short)),
				new ColumnInfo ("MinimumScale", typeof(short)),
				new ColumnInfo ("IsConcurrencyType", typeof(bool)),
				new ColumnInfo ("IsLiteralSupported", typeof(bool)),
				new ColumnInfo ("LiteralPrefix", typeof(string)),
				new ColumnInfo ("LiteralSuffix", typeof(string))
			};

			static readonly object [][] rows = {
				new object [] {"smallint", 16, 5, "smallint", null, "System.Int16", true, true,
					       false, true, true, false, true, true, false, false, null,
					       null, false, null, null, null},
				new object [] {"int", 8, 10, "int", null, "System.Int32",
					       true, true, false, true, true, false, true, true, false,
					       false, null, null, false, null, null, null},
				new object [] {"real", 13, 7, "real", null,
					       "System.Single", false, true, false, true, false, false,
					       true, true, false, false, null, null, false, null, null, null},
				new object [] {"float", 6, 53, "float({0})",
					       "number of bits used to store the mantissa", "System.Double",
					       false, true, false, true, false, false, true, true,
					       false, false, null, null, false, null, null, null},
				new object [] {"money", 9, 19, "money", null,
					       "System.Decimal", false, false, false, true, true,
					       false, true, true, false, false, null, null, false,
					       null, null, null},
				new object [] {"smallmoney", 17, 10, "smallmoney", null,
					       "System.Decimal", false, false, false, true, true, false,
					       true, true, false, false, null, null, false, null, null, null},
				new object [] {"bit", 2, 1, "bit", null, "System.Boolean",
					       false, false, false, true, false, false, true, true,
					       false, null, null, null, false, null, null, null},
				new object [] {"tinyint", 20, 3, "tinyint", null,
					       "System.SByte", true, true, false, true, true, false,
					       true, true, false, true, null, null, false, null, null, null},
				new object [] {"bigint", 0, 19, "bigint", null,
					       "System.Int64", true, true, false, true, true, false,
					       true, true, false, false, null, null, false, null, null, null},
				new object [] {"timestamp", 19, 8, "timestamp", null,
					       "System.Byte[]", false, false, false, true, false, false,
					       false, true, false, null, null, null, true, null, "0x", null},
				new object [] {"binary", 1, 8000, "binary({0})", "length",
					       "System.Byte[]", false, true, false, true, false, false,
					       true, true, false, null, null, null, false, null, "0x", null},
				new object [] {"image", 7, 2147483647, "image", null,
					       "System.Byte[]", false, true, false, false, false, true,
					       true, false, false, null, null, null, false, null, "0x", null},
				new object [] {"text", 18, 2147483647, "text", null,
					       "System.String", false, true, false, false, false, true,
					       true, false, true, null, null, null, false, null, "'", "'"},
				new object [] {"ntext", 11, 1073741823, "ntext", null,
					       "System.String", false, true, false, false, false, true,
					       true, false, true, null, null, null, false, null, "N'", "'"},
				new object [] {"decimal", 5, 38, "decimal({0}, {1})",
					       "precision,scale", "System.Decimal", true, true, false,
					       true, false, false, true, true, false, false, 38, 0,
					       false, null, null, null},
				new object [] {"numeric", 5, 38, "numeric({0}, {1})",
					       "precision,scale", "System.Decimal", true, true, false,
					       true, false, false, true, true, false, false, 38, 0,
					       false, null, null, null},
				new object [] {"datetime", 4, 23, "datetime", null,
					       "System.DateTime", false, true, false, true, false, false,
					       true, true, true, null, null, null, false, null, "{ts '", "'}"},
				new object [] {"smalldatetime", 15, 16, "smalldatetime", null,
					       "System.DateTime", false, true, false, true, false, false,
					       true, true, true, null, null, null, false, null, "{ts '", "'}"},
				new object [] {"sql_variant", 23, null, "sql_variant",
					       null, "System.Object", false, true, false, false, false,
					       false, true, true, false, null, null, null, false, false,
					       null, null},
				new object [] {"xml", 25, 2147483647, "xml", null,
					       "System.String", false, false, false, false, false, true,
					       true, false, false, null, null, null, false, false, null, null},
				new object [] {"varchar", 22, 2147483647, "varchar({0})",
					       "max length", "System.String", false, true, false, false,
					       false, false, true, true, true, null, null, null, false,
					       null, "'", "'"},
				new object [] {"char", 3, 2147483647, "char({0})", "length",
					       "System.String", false, true, false, true, false, false,
					       true, true, true, null, null, null, false, null, "'", "'"},
				new object [] {"nchar", 10, 1073741823, "nchar({0})", "length",
					       "System.String", false, true, false, true, false, false,
					       true, true, true, null, null, null, false, null, "N'", "'"},
				new object [] {"nvarchar", 12, 1073741823, "nvarchar({0})", "max length",
					       "System.String", false, true, false, false, false, false, true, true,
					       true, null, null, null, false, null, "N'", "'"},
				new object [] {"varbinary", 21, 1073741823, "varbinary({0})",
					       "max length", "System.Byte[]", false, true, false, false,
					       false, false, true, true, false, null, null, null, false,
					       null, "0x", null},
				new object [] {"uniqueidentifier", 14, 16, "uniqueidentifier", null,
					       "System.Guid", false, true, false, true, false, false, true,
					       true, false, null, null, null, false, null, "'", "'"},
				new object [] {"date", 31, 3L, "date", DBNull.Value,
					       "System.DateTime", false, false, false, true, true, false,
					       true, true, true, DBNull.Value, DBNull.Value, DBNull.Value,
					       false, DBNull.Value, "{ts '", "'}"},
				new object [] {"time", 32, 5L, "time({0})", "scale",
					       "System.TimeSpan", false, false, false, false, false, false,
					       true, true, true, DBNull.Value, (short) 7, (short) 0,
					       false, DBNull.Value, "{ts '", "'}"},
				new object [] {"datetime2", 33, 8L, "datetime2({0})", "scale",
					       "System.DateTime", false, true, false, false, false, false,
					       true, true, true, DBNull.Value, (short) 7, (short) 0,
					       false, DBNull.Value, "{ts '", "'}"},
				new object [] {"datetimeoffset", 34, 10L, "datetimeoffset({0})",
					       "scale", "System.DateTimeOffset", false, true, false, false,
					       false, false, true, true, true, DBNull.Value, (short) 7, (short) 0,
					       false, DBNull.Value, "{ts '", "'}"}
			};

			static DataTable instance;
			static public DataTable Instance {
				get {
					if (instance == null) {
						instance = new DataTable ("DataTypes");
						foreach (ColumnInfo c in columns)
							instance.Columns.Add (c.name, c.type);
						foreach (object [] row in rows)
							instance.LoadDataRow (row, true);
					}
					return instance;
				}
			}
		}

		static class Restrictions
		{
			static readonly ColumnInfo [] columns = {
				new ColumnInfo ("CollectionName", typeof (string)),
				new ColumnInfo ("RestrictionName", typeof(string)),
				new ColumnInfo ("ParameterName", typeof(string)),
				new ColumnInfo ("RestrictionDefault", typeof(string)),
				new ColumnInfo ("RestrictionNumber", typeof(int))
			};

			static readonly object [][] rows = {
				new object [] {"Users", "User_Name", "@Name", "name", 1},
				new object [] {"Databases", "Name", "@Name", "Name", 1},

				new object [] {"Tables", "Catalog", "@Catalog", "TABLE_CATALOG", 1},
				new object [] {"Tables", "Owner", "@Owner", "TABLE_SCHEMA", 2},
				new object [] {"Tables", "Table", "@Name", "TABLE_NAME", 3},
				new object [] {"Tables", "TableType", "@TableType", "TABLE_TYPE", 4},

				new object [] {"Columns", "Catalog", "@Catalog", "TABLE_CATALOG", 1},
				new object [] {"Columns", "Owner", "@Owner", "TABLE_SCHEMA", 2},
				new object [] {"Columns", "Table", "@Table", "TABLE_NAME", 3},
				new object [] {"Columns", "Column", "@Column", "COLUMN_NAME", 4},

				new object [] {"StructuredTypeMembers", "Catalog", "@Catalog", "TYPE_CATALOG", 1},
				new object [] {"StructuredTypeMembers", "Owner", "@Owner", "TYPE_SCHEMA", 2},
				new object [] {"StructuredTypeMembers", "Type", "@Type", "TYPE_NAME", 3},
				new object [] {"StructuredTypeMembers", "Member", "@Member", "MEMBER_NAME", 4},

				new object [] {"Views", "Catalog", "@Catalog", "TABLE_CATALOG", 1},
				new object [] {"Views", "Owner", "@Owner", "TABLE_SCHEMA", 2},
				new object [] {"Views", "Table", "@Table", "TABLE_NAME", 3},

				new object [] {"ViewColumns", "Catalog", "@Catalog", "VIEW_CATALOG", 1},
				new object [] {"ViewColumns", "Owner", "@Owner", "VIEW_SCHEMA", 2},
				new object [] {"ViewColumns", "Table", "@Table", "VIEW_NAME", 3},
				new object [] {"ViewColumns", "Column", "@Column", "COLUMN_NAME", 4},

				new object [] {"ProcedureParameters", "Catalog", "@Catalog", "SPECIFIC_CATALOG", 1},
				new object [] {"ProcedureParameters", "Owner", "@Owner", "SPECIFIC_SCHEMA", 2},
				new object [] {"ProcedureParameters", "Name", "@Name", "SPECIFIC_NAME", 3},
				new object [] {"ProcedureParameters", "Parameter", "@Parameter", "PARAMETER_NAME", 4},

				new object [] {"Procedures", "Catalog", "@Catalog", "SPECIFIC_CATALOG", 1},
				new object [] {"Procedures", "Owner", "@Owner", "SPECIFIC_SCHEMA", 2},
				new object [] {"Procedures", "Name", "@Name", "SPECIFIC_NAME", 3},
				new object [] {"Procedures", "Type", "@Type", "ROUTINE_TYPE", 4},

				new object [] {"IndexColumns", "Catalog", "@Catalog", "db_name()", 1},
				new object [] {"IndexColumns", "Owner", "@Owner", "user_name()", 2},
				new object [] {"IndexColumns", "Table", "@Table", "o.name", 3},
				new object [] {"IndexColumns", "ConstraintName", "@ConstraintName", "x.name", 4},
				new object [] {"IndexColumns", "Column", "@Column", "c.name", 5},

				new object [] {"Indexes", "Catalog", "@Catalog", "db_name()", 1},
				new object [] {"Indexes", "Owner", "@Owner", "user_name()", 2},
				new object [] {"Indexes", "Table", "@Table", "o.name", 3},
				new object [] {"Indexes", "Name", "@Name", "x.name", 4},

				new object [] {"UserDefinedTypes", "assembly_name", "@AssemblyName", "assemblies.name", 1},
				new object [] {"UserDefinedTypes", "udt_name", "@UDTName", "types.assembly_class", 2},

				new object [] {"ForeignKeys", "Catalog", "@Catalog", "CONSTRAINT_CATALOG", 1},
				new object [] {"ForeignKeys", "Owner", "@Owner", "CONSTRAINT_SCHEMA", 2},
				new object [] {"ForeignKeys", "Table", "@Table", "TABLE_NAME", 3},
				new object [] {"ForeignKeys", "Name", "@Name", "CONSTRAINT_NAME", 4}
			};

			static DataTable instance;
			static public DataTable Instance {
				get {
					if (instance == null) {
						instance = new DataTable ("Restrictions");
						foreach (ColumnInfo c in columns)
							instance.Columns.Add (c.name, c.type);
						foreach (object [] row in rows)
							instance.LoadDataRow (row, true);
					}
					return instance;
				}
			}
		}

		public override DataTable GetSchema ()
		{
			if (state == ConnectionState.Closed)
				throw ExceptionHelper.ConnectionClosed ();

			return MetaDataCollections.Instance;
		}

		public override DataTable GetSchema (String collectionName)
		{
			return GetSchema (collectionName, null);
		}

		public override DataTable GetSchema (String collectionName, string [] restrictionValues)
		{
			// LAMESPEC: In MS.NET, if collectionName is null, it throws ArgumentException.

			if (state == ConnectionState.Closed)
				throw ExceptionHelper.ConnectionClosed ();

			String cName = null;
			DataTable schemaTable = MetaDataCollections.Instance;
			int length = restrictionValues == null ? 0 : restrictionValues.Length;

			foreach (DataRow row in schemaTable.Rows) {
				if (String.Compare ((string) row["CollectionName"], collectionName, true) == 0) {
					if (length > (int) row["NumberOfRestrictions"]) {
						throw new ArgumentException ("More restrictions were provided " +
									     "than the requested schema ('" +
									     row["CollectionName"].ToString () + "') supports");
					}
					cName = row["CollectionName"].ToString();
				}
			}

			if (cName == null)
				throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
					"The requested collection ({0}) is not defined.",
					collectionName));

			SqlCommand command     = null;
			DataTable dataTable    = new DataTable ();
			SqlDataAdapter dataAdapter = new SqlDataAdapter ();

			switch (cName)
			{
			case "Databases":
				command = new SqlCommand ("select name as database_name, dbid, crdate as create_date " +
							  "from master.sys.sysdatabases where (name = @Name or (@Name " +
							  "is null))", this);
				command.Parameters.Add ("@Name", SqlDbType.NVarChar, 4000);
				break;
			case "ForeignKeys":
				command = new SqlCommand ("select CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, " +
							  "TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, CONSTRAINT_TYPE, " +
							  "IS_DEFERRABLE, INITIALLY_DEFERRED from " +
							  "INFORMATION_SCHEMA.TABLE_CONSTRAINTS where (CONSTRAINT_CATALOG" +
							  " = @Catalog or (@Catalog is null)) and (CONSTRAINT_SCHEMA = " +
							  "@Owner or (@Owner is null)) and (TABLE_NAME = @Table or (" +
							  "@Table is null)) and (CONSTRAINT_NAME = @Name or (@Name is null))" +
							  " and CONSTRAINT_TYPE = 'FOREIGN KEY' order by CONSTRAINT_CATALOG," +
							  " CONSTRAINT_SCHEMA, CONSTRAINT_NAME", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Table", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Name", SqlDbType.NVarChar, 4000);
				break;
			case "Indexes":
				command = new SqlCommand ("select distinct db_name() as constraint_catalog, " +
							  "constraint_schema = user_name (o.uid), " +
							  "constraint_name = x.name, table_catalog = db_name (), " +
							  "table_schema = user_name (o.uid), table_name = o.name, " +
							  "index_name  = x.name from sysobjects o, sysindexes x, " +
							  "sysindexkeys xk where o.type in ('U') and x.id = o.id and " +
							  "o.id = xk.id and x.indid = xk.indid and xk.keyno = x.keycnt " +
							  "and (db_name() = @Catalog or (@Catalog is null)) and " +
							  "(user_name() = @Owner or (@Owner is null)) and (o.name = " +
							  "@Table or (@Table is null)) and (x.name = @Name or (@Name is null))" +
							  "order by table_name, index_name", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Table", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Name", SqlDbType.NVarChar, 4000);
				break;
			case "IndexColumns":
				command = new SqlCommand ("select distinct db_name() as constraint_catalog, " +
							  "constraint_schema = user_name (o.uid), constraint_name = x.name, " +
							  "table_catalog = db_name (), table_schema = user_name (o.uid), " +
							  "table_name = o.name, column_name = c.name, " +
							  "ordinal_position = convert (int, xk.keyno), keyType = c.xtype, " +
							  "index_name = x.name from sysobjects o, sysindexes x, syscolumns c, " +
							  "sysindexkeys xk where o.type in ('U') and x.id = o.id and o.id = c.id " +
							  "and o.id = xk.id and x.indid = xk.indid and c.colid = xk.colid " +
							  "and xk.keyno <= x.keycnt and permissions (o.id, c.name) <> 0 " +
							  "and (db_name() = @Catalog or (@Catalog is null)) and (user_name() " +
							  "= @Owner or (@Owner is null)) and (o.name = @Table or (@Table is" +
							  " null)) and (x.name = @ConstraintName or (@ConstraintName is null)) " +
							  "and (c.name = @Column or (@Column is null)) order by table_name, " +
							  "index_name", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 8);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Table", SqlDbType.NVarChar, 13);
				command.Parameters.Add ("@ConstraintName", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Column", SqlDbType.NVarChar, 4000);
				break;
			case "Procedures":
				command = new SqlCommand ("select SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME, " +
							  "ROUTINE_CATALOG, ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, " +
							  "CREATED, LAST_ALTERED from INFORMATION_SCHEMA.ROUTINES where " +
							  "(SPECIFIC_CATALOG = @Catalog or (@Catalog is null)) and " +
							  "(SPECIFIC_SCHEMA = @Owner or (@Owner is null)) and (SPECIFIC_NAME" +
							  " = @Name or (@Name is null)) and (ROUTINE_TYPE = @Type or (@Type " +
							  "is null)) order by SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Name", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Type", SqlDbType.NVarChar, 4000);
				break;
			case "ProcedureParameters":
				command = new SqlCommand ("select SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME, " +
							  "ORDINAL_POSITION, PARAMETER_MODE, IS_RESULT, AS_LOCATOR, " +
							  "PARAMETER_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, " +
							  "CHARACTER_OCTET_LENGTH, COLLATION_CATALOG, COLLATION_SCHEMA, " +
							  "COLLATION_NAME, CHARACTER_SET_CATALOG, CHARACTER_SET_SCHEMA, " +
							  "CHARACTER_SET_NAME, NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, " +
							  "NUMERIC_SCALE, DATETIME_PRECISION, INTERVAL_TYPE, " +
							  "INTERVAL_PRECISION from INFORMATION_SCHEMA.PARAMETERS where " +
							  "(SPECIFIC_CATALOG = @Catalog or (@Catalog is null)) and " +
							  "(SPECIFIC_SCHEMA = @Owner or (@Owner is null)) and (SPECIFIC_NAME = " +
							  "@Name or (@Name is null)) and (PARAMETER_NAME = @Parameter or (" +
							  "@Parameter is null)) order by SPECIFIC_CATALOG, SPECIFIC_SCHEMA," +
							  " SPECIFIC_NAME, PARAMETER_NAME", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Name", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Parameter", SqlDbType.NVarChar, 4000);
				break;
			case "Tables":
				command = new SqlCommand ("select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE " +
							  "from INFORMATION_SCHEMA.TABLES where" +
							  " (TABLE_CATALOG = @catalog or (@catalog is null)) and " +
							  "(TABLE_SCHEMA = @owner or (@owner is null))and " +
							  "(TABLE_NAME = @name or (@name is null)) and " +
							  "(TABLE_TYPE = @table_type or (@table_type is null))", this);
				command.Parameters.Add ("@catalog", SqlDbType.NVarChar, 8);
				command.Parameters.Add ("@owner", SqlDbType.NVarChar, 3);
				command.Parameters.Add ("@name", SqlDbType.NVarChar, 11);
				command.Parameters.Add ("@table_type", SqlDbType.NVarChar, 10);
				break;
			case "Columns":
				command = new SqlCommand ("select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, " +
							  "ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, " +
							  "CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, " +
							  "NUMERIC_PRECISION, NUMERIC_PRECISION_RADIX, NUMERIC_SCALE, " +
							  "DATETIME_PRECISION, CHARACTER_SET_CATALOG, CHARACTER_SET_SCHEMA, " +
							  "CHARACTER_SET_NAME, COLLATION_CATALOG from INFORMATION_SCHEMA.COLUMNS" +
							  " where (TABLE_CATALOG = @Catalog or (@Catalog is null)) and (" +
							  "TABLE_SCHEMA = @Owner or (@Owner is null)) and (TABLE_NAME = @table" +
							  " or (@Table is null)) and (COLUMN_NAME = @column or (@Column is null" +
							  ")) order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Table", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Column", SqlDbType.NVarChar, 4000);
				break;
			case "Users":
				command = new SqlCommand ("select uid, name as user_name, createdate, updatedate from sysusers" +
							  " where (name = @Name or (@Name is null))", this);
				command.Parameters.Add ("@Name", SqlDbType.NVarChar, 4000);
				break;
			case "StructuredTypeMembers":
				// Only available on SQL Server 2008
				// Running it again SQL 2005 results in the following exception:
				// Unable to build the 'StructuredTypeMembers' collection because
				// execution of the SQL query failed. See the inner exception for details.
				// ---> System.Data.SqlClient.SqlException: Invalid object name 'sys.table_types'.
				// 
				// I don't have access to SQL Server 2008 right now,
				// and can't find any online documentation on the 'sys.table_types'
				// view
				throw new NotImplementedException ();
			case "Views":
				command = new SqlCommand ("select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, CHECK_OPTION, " +
							  "IS_UPDATABLE from INFORMATION_SCHEMA.VIEWS where (TABLE_CATALOG" +
							  " = @Catalog or (@Catalog is null)) TABLE_SCHEMA = @Owner or " +
							  "(@Owner is null)) and (TABLE_NAME = @table or (@Table is null))" +
							  " order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Table", SqlDbType.NVarChar, 4000);
				break;
			case "ViewColumns":
				command = new SqlCommand ("select VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME, TABLE_CATALOG, " +
							  "TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from " +
							  "INFORMATION_SCHEMA.VIEW_COLUMN_USAGE where (VIEW_CATALOG = " +
							  "@Catalog (@Catalog is null)) and (VIEW_SCHEMA = @Owner (@Owner" +
							  " is null)) and (VIEW_NAME = @Table or (@Table is null)) and " +
							  "(COLUMN_NAME = @Column or (@Column is null)) order by " +
							  "VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME", this);
				command.Parameters.Add ("@Catalog", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Owner", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Table", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@Column", SqlDbType.NVarChar, 4000);
				break;
			case "UserDefinedTypes":
				command = new SqlCommand ("select assemblies.name as assembly_name, types.assembly_class " +
							  "as udt_name, ASSEMBLYPROPERTY(assemblies.name, 'VersionMajor') " +
							  "as version_major, ASSEMBLYPROPERTY(assemblies.name, 'VersionMinor') " +
							  "as version_minor, ASSEMBLYPROPERTY(assemblies.name, 'VersionBuild') " +
							  "as version_build, ASSEMBLYPROPERTY(assemblies.name, 'VersionRevision') " +
							  "as version_revision, ASSEMBLYPROPERTY(assemblies.name, 'CultureInfo') " +
							  "as culture_info, ASSEMBLYPROPERTY(assemblies.name, 'PublicKey') " +
							  "as public_key, is_fixed_length, max_length, Create_Date, " +
							  "Permission_set_desc from sys.assemblies as assemblies join " +
							  "sys.assembly_types as types on assemblies.assembly_id = types.assembly_id" +
							  " where (assportemblies.name = @AssemblyName or (@AssemblyName is null)) and " +
							  "(types.assembly_class = @UDTName or (@UDTName is null))",
							  this);
				command.Parameters.Add ("@AssemblyName", SqlDbType.NVarChar, 4000);
				command.Parameters.Add ("@UDTName", SqlDbType.NVarChar, 4000);
				break;
			case "MetaDataCollections":
				return MetaDataCollections.Instance;
			case "DataSourceInformation":
				return DataSourceInformation.GetInstance (this);
			case "DataTypes":
				return DataTypes.Instance;
			case "ReservedWords":
				return ReservedWords.Instance;
			case "Restrictions":
				return Restrictions.Instance;
			}
			for (int i = 0; i < length; i++) {
				command.Parameters[i].Value = restrictionValues[i];
			}
			dataAdapter.SelectCommand = command;
			dataAdapter.Fill (dataTable);
			return dataTable;
		}
		
		public static void ChangePassword (string connectionString, string newPassword)
		{
			if (String.IsNullOrEmpty (connectionString))
				throw new ArgumentNullException ("The 'connectionString' cannot be null or empty.");
			if (String.IsNullOrEmpty (newPassword))
				throw new ArgumentNullException ("The 'newPassword' cannot be null or empty.");
			if (newPassword.Length > 128)
				throw new ArgumentException ("The length of 'newPassword' cannot exceed 128 characters.");
			using (SqlConnection conn = new SqlConnection (connectionString)) {
				conn.Open ();
				conn.tds.Execute (String.Format ("sp_password '{0}', '{1}', '{2}'",
								 conn.parms.Password, newPassword, conn.parms.User));
			}
		}

		public static void ClearAllPools ()
		{
			// FIXME: locking
			IDictionary pools = SqlConnection.sqlConnectionPools.GetConnectionPool ();
			foreach (TdsConnectionPool pool in pools.Values) {
				if (pool != null)
					pool.ResetConnectionPool ();
			}
			pools.Clear ();
		}

		public static void ClearPool (SqlConnection connection)
		{
			if (connection == null)
				throw new ArgumentNullException ("connection");

			// FIXME: locking
			if (connection.pooling) {
				TdsConnectionPool pool = sqlConnectionPools.GetConnectionPool (connection.ConnectionString);
				if (pool != null)
					pool.ResetConnectionPool ();
			}
		}

#endif // NET_2_0

		#endregion // Methods

#if NET_2_0
		#region Fields Net 2

		bool async;
		bool userInstance;

		#endregion // Fields  Net 2

		#region Properties Net 2

#if NET_1_0
		[DataSysDescription ("Enable Asynchronous processing, 'Asynchrouse Processing=true/false' in the ConnectionString.")]	
#endif
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		internal bool AsyncProcessing  {
			get { return async; }
		}

		#endregion // Properties Net 2

#endif // NET_2_0

	}
}