2 // Mono.Data.TdsClient.TdsConnection.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) 2002 Tim Coleman
10 using Mono.Data.TdsClient.Internal;
12 using System.Collections;
13 using System.Collections.Specialized;
14 using System.ComponentModel;
19 namespace Mono.Data.TdsClient {
20 public class TdsConnection : Component, ICloneable, IDbConnection
24 bool autoCommit = true;
25 bool disablePooling = false;
26 string connectionString = null;
27 int connectionTimeout = 15;
29 IsolationLevel isolationLevel = IsolationLevel.ReadCommitted;
32 ConnectionState state = ConnectionState.Closed;
34 TdsConnectionParameters parms = new TdsConnectionParameters ();
35 TdsTransaction transaction = null;
37 // This is the collection of connection pools available
38 static Hashtable pools = new Hashtable ();
40 // Our TDS object, the real workhorse
47 public TdsConnection ()
52 public TdsConnection (string connectionString)
54 parms.PacketSize = 512;
55 parms.TdsVersion = TdsVersion.tds42;
57 parms.Password = null;
58 SetConnectionString (connectionString);
61 #endregion // Constructors
65 public string ConnectionString {
66 get { return connectionString; }
67 set { SetConnectionString (value); }
70 public string Database {
71 get { return parms.Database; }
74 public string DataSource {
75 get { return parms.DataSource; }
78 public ConnectionState State {
82 public int ConnectionTimeout {
83 get { return connectionTimeout; }
86 public int PacketSize {
87 get { return parms.PacketSize; }
91 get { return parms.User; }
94 public string Password {
95 get { return parms.Password; }
102 #endregion // Properties
106 private static Tds AllocateTds (TdsConnectionParameters parms, string connectionString, bool disablePooling, int minPoolSize, int maxPoolSize)
109 return new Tds (parms);
111 TdsConnectionPool pool = (TdsConnectionPool) pools[connectionString];
114 pool = new TdsConnectionPool (parms, minPoolSize, maxPoolSize);
115 pools[connectionString] = pool;
119 return pool.FindAnAvailableTds ();
122 public TdsTransaction BeginTransaction ()
124 return BeginTransaction (IsolationLevel.ReadCommitted);
127 public TdsTransaction BeginTransaction (IsolationLevel il)
129 if (state == ConnectionState.Closed)
130 throw new InvalidOperationException ("Invalid operation. The connection is closed.");
131 if (transaction != null && transaction.Open)
132 throw new InvalidOperationException ("TdsConnection does not support parallel transactions.");
134 transaction = new TdsTransaction (this, il);
138 public void ChangeDatabase (string databaseName)
140 if (Database == databaseName)
143 tds.ChangeDatabase (databaseName);
148 // rollback any open transactions
149 if (transaction.Open)
150 transaction.Rollback ();
152 // if we aren't pooling, just close the connection
153 // otherwise, just set the InUse flag to false
158 this.state = ConnectionState.Closed;
161 public TdsCommand CreateCommand ()
163 TdsCommand command = new TdsCommand ();
164 command.Connection = this;
168 object ICloneable.Clone()
170 throw new NotImplementedException ();
173 IDbTransaction IDbConnection.BeginTransaction ()
175 return BeginTransaction ();
178 IDbTransaction IDbConnection.BeginTransaction (IsolationLevel il)
180 return BeginTransaction (il);
183 IDbCommand IDbConnection.CreateCommand ()
185 return CreateCommand ();
190 if (connectionString == null)
191 throw new InvalidOperationException ("The ConnectionString property has not been initialized.");
194 throw new ArgumentException ();
196 if (Password == null)
198 throw new ArgumentException ();
201 tds = AllocateTds (parms, connectionString, disablePooling, minPoolSize, maxPoolSize);
204 this.state = ConnectionState.Open;
208 private void SetConnectionString (string connectionString)
210 connectionString += ";";
211 NameValueCollection parameters = new NameValueCollection ();
213 if (connectionString == String.Empty)
216 bool inQuote = false;
217 bool inDQuote = false;
219 string name = String.Empty;
220 string value = String.Empty;
221 StringBuilder sb = new StringBuilder ();
223 foreach (char c in connectionString)
230 inDQuote = !inDQuote;
233 if (!inDQuote && !inQuote) {
234 value = sb.ToString ();
235 sb = new StringBuilder ();
241 if (!inDQuote && !inQuote) {
242 name = sb.ToString ();
243 sb = new StringBuilder ();
252 if (value != String.Empty) {
253 parameters [name.ToUpper ().Trim ()] = value.Trim ();
255 value = String.Empty;
259 if (this.ConnectionString == null)
261 SetDefaultConnectionParameters (parameters);
264 SetProperties (parameters);
266 this.connectionString = connectionString;
270 private void SetDefaultConnectionParameters (NameValueCollection parameters)
272 if (null == parameters.Get ("APPLICATION NAME"))
273 parameters["APPLICATION NAME"] = ".Net SqlClient Data Provider";
274 if (null == parameters.Get ("CONNECT TIMEOUT") && null == parameters.Get ("CONNECTION TIMEOUT"))
275 parameters["CONNECT TIMEOUT"] = "15";
276 if (null == parameters.Get ("CONNECTION LIFETIME"))
277 parameters["CONNECTION LIFETIME"] = "0";
278 if (null == parameters.Get ("CONNECTION RESET"))
279 parameters["CONNECTION RESET"] = "true";
280 if (null == parameters.Get ("ENLIST"))
281 parameters["ENLIST"] = "true";
282 if (null == parameters.Get ("INTEGRATED SECURITY") && null == parameters.Get ("TRUSTED_CONNECTION"))
283 parameters["INTEGRATED SECURITY"] = "false";
284 if (null == parameters.Get ("MAX POOL SIZE"))
285 parameters["MAX POOL SIZE"] = "100";
286 if (null == parameters.Get ("MIN POOL SIZE"))
287 parameters["MIN POOL SIZE"] = "0";
288 if (null == parameters.Get ("NETWORK LIBRARY") && null == parameters.Get ("NET"))
289 parameters["NETWORK LIBRARY"] = "dbmssocn";
290 if (null == parameters.Get ("PACKET SIZE"))
291 parameters["PACKET SIZE"] = "8192";
292 if (null == parameters.Get ("PERSIST SECURITY INFO"))
293 parameters["PERSIST SECURITY INFO"] = "false";
294 if (null == parameters.Get ("POOLING"))
295 parameters["POOLING"] = "true";
296 if (null == parameters.Get ("WORKSTATION ID"))
297 parameters["WORKSTATION ID"] = Dns.GetHostByName ("localhost").HostName;
300 private void SetProperties (NameValueCollection parameters)
303 foreach (string name in parameters) {
304 value = parameters[name];
307 case "APPLICATION NAME" :
308 parms.ApplicationName = value;
310 case "ATTACHDBFILENAME" :
311 case "EXTENDED PROPERTIES" :
312 case "INITIAL FILE NAME" :
314 case "CONNECT TIMEOUT" :
315 case "CONNECTION TIMEOUT" :
316 connectionTimeout = Int32.Parse (value);
318 case "CONNECTION LIFETIME" :
320 case "CONNECTION RESET" :
322 case "CURRENT LANGUAGE" :
323 parms.Language = value;
329 case "NETWORK ADDRESS" :
330 parms.DataSource = value;
334 case "INITIAL CATALOG" :
336 parms.Database = value;
338 case "INTEGRATED SECURITY" :
339 case "TRUSTED_CONNECTION" :
341 case "MAX POOL SIZE" :
342 maxPoolSize = Int32.Parse (value);
344 case "MIN POOL SIZE" :
345 minPoolSize = Int32.Parse (value);
348 case "NETWORK LIBRARY" :
349 if (!value.ToUpper ().Equals ("DBMSSOCN"))
350 throw new TdsException ("Unsupported network library.");
353 parms.PacketSize = Int32.Parse (value);
357 parms.Password = value;
359 case "PERSIST SECURITY INFO" :
362 disablePooling = (value.ToUpper ().Equals ("FALSE") || value.ToUpper ().Equals ("NO"));
367 case "WORKSTATION ID" :
368 parms.Hostname = value;
373 #endregion // Methods