1 // ByteFX.Data data access components for .Net
\r
2 // Copyright (C) 2002-2003 ByteFX, Inc.
\r
4 // This library is free software; you can redistribute it and/or
\r
5 // modify it under the terms of the GNU Lesser General Public
\r
6 // License as published by the Free Software Foundation; either
\r
7 // version 2.1 of the License, or (at your option) any later version.
\r
9 // This library is distributed in the hope that it will be useful,
\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
12 // Lesser General Public License for more details.
\r
14 // You should have received a copy of the GNU Lesser General Public
\r
15 // License along with this library; if not, write to the Free Software
\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
19 using System.Collections;
\r
22 namespace ByteFX.Data.Common
\r
25 /// Summary description for Utility.
\r
27 internal abstract class DBConnectionString
\r
29 protected Hashtable keyValues = new Hashtable();
\r
30 protected string connectionName = String.Empty;
\r
31 protected string connectString;
\r
33 public DBConnectionString()
\r
35 keyValues = GetDefaultValues();
\r
38 public void SetConnectionString(string value)
\r
40 Hashtable ht = Parse( value );
\r
41 connectString = value;
\r
45 protected string GetString(string name)
\r
47 if (! keyValues.ContainsKey(name)) return String.Empty;
\r
48 return (keyValues[name] as string);
\r
51 protected int GetInt( string name )
\r
53 return Convert.ToInt32(keyValues[name]);
\r
56 protected bool GetBool( string name )
\r
58 return Convert.ToBoolean(keyValues[name]);
\r
61 protected virtual bool ConnectionParameterParsed(Hashtable hash, string key, string value)
\r
63 switch (key.ToLower())
\r
65 case "persist security info":
\r
66 hash["persist security info"] =
\r
67 value.ToLower() == "yes" || value.ToLower() == "true";
\r
75 hash["user id"] = value;
\r
80 hash["password"] = value;
\r
89 case "network address":
\r
90 hash["host"] = value;
\r
93 case "initial catalog":
\r
95 hash["database"] = value;
\r
98 case "connection timeout":
\r
99 case "connect timeout":
\r
100 hash["connect timeout"] = Int32.Parse( value );
\r
104 hash["port"] = Int32.Parse( value );
\r
109 value.ToLower() == "yes" || value.ToLower() == "true";
\r
112 case "min pool size":
\r
113 hash["min pool size"] = Int32.Parse(value);
\r
116 case "max pool size":
\r
117 hash["max pool size"] = Int32.Parse(value);
\r
120 case "connection lifetime":
\r
121 hash["connect lifetime"] = Int32.Parse(value);
\r
127 protected virtual Hashtable GetDefaultValues()
\r
132 protected Hashtable ParseKeyValuePairs( string src )
\r
134 String[] keyvalues = src.Split( ';' );
\r
135 String[] newkeyvalues = new String[keyvalues.Length];
\r
138 // first run through the array and check for any keys that
\r
139 // have ; in their value
\r
140 foreach (String keyvalue in keyvalues)
\r
142 // check for trailing ; at the end of the connection string
\r
143 if (keyvalue.Length == 0) continue;
\r
145 // this value has an '=' sign so we are ok
\r
146 if (keyvalue.IndexOf('=') >= 0)
\r
148 newkeyvalues[x++] = keyvalue;
\r
152 newkeyvalues[x-1] += ";";
\r
153 newkeyvalues[x-1] += keyvalue;
\r
157 Hashtable hash = new Hashtable();
\r
159 // now we run through our normalized key-values, splitting on equals
\r
160 for (int y=0; y < x; y++)
\r
162 String[] parts = newkeyvalues[y].Split( '=' );
\r
164 // first trim off any space and lowercase the key
\r
165 parts[0] = parts[0].Trim().ToLower();
\r
166 parts[1] = parts[1].Trim();
\r
168 // we also want to clear off any quotes
\r
169 parts[0] = parts[0].Trim('\'', '"');
\r
170 parts[1] = parts[1].Trim('\'', '"');
\r
172 hash.Add( parts[0], parts[1] );
\r
177 protected virtual Hashtable Parse(string newConnectString)
\r
179 Hashtable hash = ParseKeyValuePairs( newConnectString );
\r
180 Hashtable newHash = GetDefaultValues();
\r
182 foreach (object key in hash.Keys)
\r
183 ConnectionParameterParsed( newHash, (string)key, (string)hash[key] );
\r