1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 using System
.Collections
;
22 namespace ByteFX
.Data
.Common
25 /// Summary description for Utility.
27 internal class DBConnectionString
29 protected Hashtable keyValues
= new Hashtable();
30 protected string connectString
;
31 protected string host
;
32 protected string username
;
33 protected string password
;
34 protected string database
;
35 protected int connectTimeout
;
37 protected int maxPoolSize
;
38 protected int minPoolSize
;
39 protected int connectLifetime
;
40 protected bool pooling
;
41 protected bool persistSecurityInfo
;
43 public DBConnectionString()
45 persistSecurityInfo
= false;
48 public DBConnectionString(string connectString
) : this()
50 this.connectString
= connectString
;
59 public string Username
61 get { return username; }
64 public string Password
66 get { return password; }
69 public int ConnectTimeout
71 get { return connectTimeout; }
74 public string Database
76 get { return database; }
84 public int MaxPoolSize
86 get { return maxPoolSize; }
89 public int MinPoolSize
91 get { return minPoolSize; }
96 get { return pooling; }
99 public int ConnectionLifetime
101 get { return connectLifetime; }
104 public string ConnectString
106 get { return GetConnectionString(); }
107 set { connectString = value; Parse(); }
112 private string GetConnectionString()
114 StringBuilder str
= new StringBuilder();
116 foreach ( string key
in keyValues
.Keys
)
118 if ((key
.ToLower() == "pwd" || key
.ToLower() == "password") &&
119 !persistSecurityInfo
) continue;
120 str
.AppendFormat("{0}={1};", key
, keyValues
[key
]);
124 str
.Remove( str
.Length
-1, 1 );
126 return str
.ToString();
129 protected virtual void ConnectionParameterParsed(string key
, string value)
131 switch (key
.ToLower())
133 case "persist security info":
134 if (value.ToLower() == "no" || value.ToLower() == "false")
135 persistSecurityInfo
= false;
137 persistSecurityInfo
= true;
159 case "network address":
163 case "initial catalog":
168 case "connection timeout":
169 case "connect timeout":
170 connectTimeout
= Int32
.Parse( value );
174 port
= Int32
.Parse( value );
178 if (value.ToLower() == "no" || value.ToLower() == "false")
184 case "min pool size":
185 minPoolSize
= Int32
.Parse(value);
188 case "max pool size":
189 maxPoolSize
= Int32
.Parse(value);
192 case "connection lifetime":
193 connectLifetime
= Int32
.Parse(value);
198 protected void Parse()
200 String
[] keyvalues
= connectString
.Split( ';' );
201 String
[] newkeyvalues
= new String
[keyvalues
.Length
];
204 // first run through the array and check for any keys that
205 // have ; in their value
206 foreach (String keyvalue
in keyvalues
)
208 // check for trailing ; at the end of the connection string
209 if (keyvalue
.Length
== 0) continue;
211 // this value has an '=' sign so we are ok
212 if (keyvalue
.IndexOf('=') >= 0)
214 newkeyvalues
[x
++] = keyvalue
;
218 newkeyvalues
[x
-1] += ";";
219 newkeyvalues
[x
-1] += keyvalue
;
225 // now we run through our normalized key-values, splitting on equals
226 for (int y
=0; y
< x
; y
++)
228 String
[] parts
= newkeyvalues
[y
].Split( '=' );
230 // first trim off any space and lowercase the key
231 parts
[0] = parts
[0].Trim().ToLower();
232 parts
[1] = parts
[1].Trim();
234 // we also want to clear off any quotes
235 parts
[0] = parts
[0].Trim('\'', '"');
236 parts
[1] = parts
[1].Trim('\'', '"');
238 ConnectionParameterParsed( parts
[0], parts
[1] );
239 keyValues
.Add( parts
[0], parts
[1] );