/// <summary> /// Returns the best alias for <paramref name="keyword"/> or null if there are no known aliases. This is because some builders allow multiple keys for changing the same underlying /// property. /// </summary> /// <param name="keyword"></param> /// <param name="value"></param> /// <returns></returns> private string GetCollisionWithKeyword(string keyword, string value) { //lets evaluate this alleged keyword! _builder.Clear(); try { //Make sure it is supported by the connection string builder _builder.Add(keyword, value); }catch (Exception ex) { //don't output the value since that could be a password throw new ArgumentException(string.Format(FAnsiStrings.ConnectionStringKeyword_ValueNotSupported, keyword), ex); } //now iterate all the keys we had before and add those too, if the key count doesn't change for any of them we know it's a duplicate semantically if (_builder.Keys != null) { foreach (var current in _keywords) { int keysBefore = _builder.Keys.Count; _builder.Add(current.Key, current.Value.Item1); //key count in builder didn't change dispite there being new values added if (_builder.Keys.Count == keysBefore) { return(current.Key); } } } //no collisions return(null); }
/// <summary> /// This function returns login and password of user for a passed NpgsqlConnection /// </summary> /// <param name="connection">the current opened DbConnection</param> /// <param name="login">returned login corresponding to the NpgsqlConnection passed</param> /// <param name="password">returned password corresponding to the NpgsqlConnection passed</param> /// <returns>true if succeed, false otherwise (connection null or not opened)</returns> public static bool GetConnectionInformationsFrom( IDbConnection connection, out string login, out string password) { login = string.Empty; password = string.Empty; if ((connection != null) && (connection.State == System.Data.ConnectionState.Open)) { DbConnectionStringBuilder builder = new DbConnectionStringBuilder(); builder.ConnectionString = connection.ConnectionString; if (builder != null) { object value = null; bool result = builder.TryGetValue("User Id", out value); if (result) { login = value.ToString(); } result &= builder.TryGetValue("Password", out value); if (result) { password = value.ToString(); } builder.Clear(); return(result); } } return(false); }
// <Snippet1> static void Main() { DbConnectionStringBuilder builder = new DbConnectionStringBuilder(); builder.ConnectionString = @"Data Source=c:\MyData\MyDb.mdb"; builder.Add("Provider", "Microsoft.Jet.Oledb.4.0"); builder.Add("Jet OLEDB:Database Password", "*******"); builder.Add("Jet OLEDB:System Database", @"c:\MyData\Workgroup.mdb"); // Set up row-level locking. builder.Add("Jet OLEDB:Database Locking Mode", 1); // The DbConnectionStringBuilder class // is database agnostic, so it's possible to // build any type of connection string using // this class. // The ConnectionString property may have been // formatted by the DbConnectionStringBuilder class. OleDbConnection oledbConnect = new OleDbConnection(builder.ConnectionString); Console.WriteLine(oledbConnect.ConnectionString); // Use the same DbConnectionStringBuilder to create // a SqlConnection object. builder.Clear(); builder.Add("integrated security", true); builder.Add("Initial Catalog", "AdventureWorks"); builder.Add("Data Source", "(local)"); SqlConnection sqlConnect = new SqlConnection(builder.ConnectionString); Console.WriteLine(sqlConnect.ConnectionString); // Pass the DbConnectionStringBuilder an existing // connection string, and you can retrieve and // modify any of the elements. builder.ConnectionString = "server=(local);user id=*******;" + "password=*******;initial catalog=AdventureWorks"; builder["Server"] = "."; builder.Remove("User ID"); // Note that calling Remove on a nonexistent item doesn't // throw an exception. builder.Remove("BadItem"); // Setting the indexer adds the value if // necessary. builder["Integrated Security"] = true; builder.Remove("password"); builder["User ID"] = "Hello"; Console.WriteLine(builder.ConnectionString); Console.WriteLine("Press Enter to finish."); Console.ReadLine(); }
// <Snippet1> static void Main() { // Create a new DbConnctionStringBuilder, and add items // to the internal collection of key/value pairs. DbConnectionStringBuilder builder = new DbConnectionStringBuilder(); builder.Add("Data Source", @"c:\MyData\MyDb.mdb"); builder.Add("Provider", "Microsoft.Jet.Oledb.4.0"); builder.Add("Jet OLEDB:Database Password", "********"); builder.Add("Jet OLEDB:System Database", @"c:\MyData\Workgroup.mdb"); // Set up row-level locking. builder.Add("Jet OLEDB:Database Locking Mode", 1); // Display the contents of the connection string, which // will now contain all the key/value pairs delimited with // semicolons. Console.WriteLine(builder.ConnectionString); Console.WriteLine(); // Clear the DbConnectionStringBuilder, and assign a complete // connection string to it, to demonstrate how // the class parses connection strings. builder.Clear(); builder.ConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;" + "Integrated Security=SSPI"; // The DbConnectionStringBuilder class has parsed the contents, // so you can work with any individual key/value pair. builder["Data Source"] = "."; Console.WriteLine(builder.ConnectionString); Console.WriteLine(); // Because the DbConnectionStringBuilder class doesn't // validate its key/value pairs, you can use this class // to store any semicolon-delimited list. The following // snippet places an arbitrary string into the ConnectionString // property, changes one of the values, and then displays the // resulting string. builder.Clear(); builder.ConnectionString = "Value1=10;Value2=20;Value3=30;Value4=40"; builder["Value2"] = 25; Console.WriteLine(builder.ConnectionString); Console.WriteLine(); builder.Clear(); try { // Assigning an invalid connection string // throws an ArgumentException. builder.ConnectionString = "xxx"; } catch (ArgumentException) { Console.WriteLine("Invalid connection string."); } Console.WriteLine(); Console.WriteLine("Press Enter to finish."); Console.ReadLine(); }
/// <summary> /// Clears the contents of the connection string /// </summary> public void ClearConnectionString() { _builder.Clear(); }