2 // System.Data.Common.DbConnectionString
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2003
12 using System.Collections;
13 using System.Collections.Specialized;
15 using System.Runtime.Serialization;
18 namespace System.Data.Common {
19 public class DbConnectionString : ISerializable
23 KeyRestrictionBehavior behavior;
24 string normalizedConnectionString;
25 internal NameValueCollection options;
32 protected internal DbConnectionString (DbConnectionString constr)
34 options = constr.options;
38 public DbConnectionString (string connectionString)
40 options = new NameValueCollection ();
41 ParseConnectionString (connectionString);
45 protected DbConnectionString (SerializationInfo si, StreamingContext sc)
50 public DbConnectionString (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
51 : this (connectionString)
53 this.behavior = behavior;
56 #endregion // Constructors
60 public KeyRestrictionBehavior Behavior {
61 get { return behavior; }
65 protected virtual string CacheConnectionString {
66 get { throw new NotImplementedException (); }
71 get { throw new NotImplementedException (); }
74 public string this [string x] {
75 get { return options [x]; }
78 public ICollection Keys {
79 get { return options.Keys; }
82 public string NormalizedConnectionString {
83 get { return normalizedConnectionString; }
87 public string Restrictions {
88 get { throw new NotImplementedException (); }
91 #endregion // Properties
95 public static void AppendKeyValuePairBuilder (StringBuilder builder, string keyname, string keyvalue)
97 throw new NotImplementedException ();
100 protected void BuildConnectionString (StringBuilder builder, string[] withoutOptions, string insertValue)
102 throw new NotImplementedException ();
105 public bool ContainsKey (string keyword)
107 return (options.Get (keyword) != null);
110 public bool ConvertValueToBoolean (string keyname, bool defaultvalue)
112 if (ContainsKey (keyname))
113 return Boolean.Parse (this [keyname].Trim ());
117 public int ConvertValueToInt32 (string keyname, int defaultvalue)
119 if (ContainsKey (keyname))
120 return Int32.Parse (this [keyname].Trim ());
124 public bool ConvertValueToIntegratedSecurity ()
126 throw new NotImplementedException ();
129 public string ConvertValueToString (string keyname, string defaultValue)
131 if (ContainsKey (keyname))
132 return this [keyname];
136 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
138 throw new NotImplementedException ();
141 protected virtual string KeywordLookup (string keyname)
146 protected void ParseConnectionString (string connectionString)
148 if (connectionString.Length == 0)
151 connectionString += ";";
153 bool inQuote = false;
154 bool inDQuote = false;
157 string name = String.Empty;
158 string value = String.Empty;
159 StringBuilder sb = new StringBuilder ();
161 for (int i = 0; i < connectionString.Length; i += 1) {
162 char c = connectionString [i];
164 if (i == connectionString.Length - 1)
167 peek = connectionString [i + 1];
173 else if (peek.Equals (c)) {
183 else if (peek.Equals (c)) {
188 inDQuote = !inDQuote;
191 if (inDQuote || inQuote)
194 if (name != String.Empty && name != null) {
195 value = sb.ToString ();
196 options [KeywordLookup (name.Trim ())] = value;
200 value = String.Empty;
201 sb = new StringBuilder ();
205 if (inDQuote || inQuote || !inName)
207 else if (peek.Equals (c)) {
212 name = sb.ToString ();
213 sb = new StringBuilder ();
218 if (inQuote || inDQuote)
220 else if (sb.Length > 0 && !peek.Equals (';'))
229 StringBuilder normalized = new StringBuilder ();
230 ArrayList keys = new ArrayList ();
231 keys.AddRange (Keys);
233 foreach (string key in keys)
235 string entry = String.Format ("{0}=\"{1}\";", key, this [key].Replace ("\"", "\"\""));
236 normalized.Append (entry);
238 normalizedConnectionString = normalized.ToString ();
241 public virtual void PermissionDemand ()
243 throw new NotImplementedException ();
246 public static string RemoveKeyValuePairs (string connectionString, string[] keynames)
248 throw new NotImplementedException ();
251 public string UsersConnectionString (bool hisPasswordPwd)
253 throw new NotImplementedException ();
256 #endregion // Methods