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 abstract class DBConnectionString
29 protected Hashtable keyValues
= new Hashtable();
30 protected string connectionName
= String
.Empty
;
31 protected string connectString
;
33 public DBConnectionString()
35 keyValues
= GetDefaultValues();
38 public void SetConnectionString(string value)
40 Hashtable ht
= Parse( value );
41 connectString
= value;
45 protected string GetString(string name
)
47 if (! keyValues
.ContainsKey(name
)) return String
.Empty
;
48 return (keyValues
[name
] as string);
51 protected int GetInt( string name
)
53 return Convert
.ToInt32(keyValues
[name
]);
56 protected bool GetBool( string name
)
58 return Convert
.ToBoolean(keyValues
[name
]);
61 protected virtual bool ConnectionParameterParsed(Hashtable hash
, string key
, string value)
63 switch (key
.ToLower())
65 case "persist security info":
66 hash
["persist security info"] =
67 value.ToLower() == "yes" || value.ToLower() == "true";
75 hash
["user id"] = value;
80 hash
["password"] = value;
89 case "network address":
93 case "initial catalog":
95 hash
["database"] = value;
98 case "connection timeout":
99 case "connect timeout":
100 hash
["connect timeout"] = Int32
.Parse( value );
104 hash
["port"] = Int32
.Parse( value );
109 value.ToLower() == "yes" || value.ToLower() == "true";
112 case "min pool size":
113 hash
["min pool size"] = Int32
.Parse(value);
116 case "max pool size":
117 hash
["max pool size"] = Int32
.Parse(value);
120 case "connection lifetime":
121 hash
["connect lifetime"] = Int32
.Parse(value);
127 protected virtual Hashtable
GetDefaultValues()
132 protected Hashtable
ParseKeyValuePairs( string src
)
134 String
[] keyvalues
= src
.Split( ';' );
135 String
[] newkeyvalues
= new String
[keyvalues
.Length
];
138 // first run through the array and check for any keys that
139 // have ; in their value
140 foreach (String keyvalue
in keyvalues
)
142 // check for trailing ; at the end of the connection string
143 if (keyvalue
.Length
== 0) continue;
145 // this value has an '=' sign so we are ok
146 if (keyvalue
.IndexOf('=') >= 0)
148 newkeyvalues
[x
++] = keyvalue
;
152 newkeyvalues
[x
-1] += ";";
153 newkeyvalues
[x
-1] += keyvalue
;
157 Hashtable hash
= new Hashtable();
159 // now we run through our normalized key-values, splitting on equals
160 for (int y
=0; y
< x
; y
++)
162 String
[] parts
= newkeyvalues
[y
].Split( '=' );
164 // first trim off any space and lowercase the key
165 parts
[0] = parts
[0].Trim().ToLower();
166 parts
[1] = parts
[1].Trim();
168 // we also want to clear off any quotes
169 parts
[0] = parts
[0].Trim('\'', '"');
170 parts
[1] = parts
[1].Trim('\'', '"');
172 hash
.Add( parts
[0], parts
[1] );
177 protected virtual Hashtable
Parse(string newConnectString
)
179 Hashtable hash
= ParseKeyValuePairs( newConnectString
);
180 Hashtable newHash
= GetDefaultValues();
182 foreach (object key
in hash
.Keys
)
183 ConnectionParameterParsed( newHash
, (string)key
, (string)hash
[key
] );