/// <summary> /// Instantiates the object that holds the credentials for accessing SQL Servers /// containing the shard map manager data. /// </summary> /// <param name="connectionString">Connection string for Shard map manager data source.</param> public SqlShardMapManagerCredentials(string connectionString) { ExceptionUtils.DisallowNullArgument(connectionString, "connectionString"); // Devnote: If connection string specifies Active Directory authentication and runtime is not // .NET 4.6 or higher, then below call will throw. SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connectionString); #region GSM Validation // DataSource must be set. if (string.IsNullOrEmpty(connectionStringBuilder.DataSource)) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._SqlShardMapManagerCredentials_ConnectionStringPropertyRequired, "DataSource"), "connectionString"); } // InitialCatalog must be set. if (string.IsNullOrEmpty(connectionStringBuilder.InitialCatalog)) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._SqlShardMapManagerCredentials_ConnectionStringPropertyRequired, "Initial Catalog"), "connectionString"); } // Ensure credentials are specified for GSM connectivity. SqlShardMapManagerCredentials.EnsureCredentials(connectionStringBuilder, "connectionString"); #endregion GSM Validation // Copy the input connection strings. _connectionStringShardMapManager = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); _connectionStringShardMapManager.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( _connectionStringShardMapManager.ApplicationName, GlobalConstants.ShardMapManagerInternalConnectionSuffixGlobal); _connectionStringShard = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); _connectionStringShard.Remove("Data Source"); _connectionStringShard.Remove("Initial Catalog"); _connectionStringShard.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( _connectionStringShard.ApplicationName, GlobalConstants.ShardMapManagerInternalConnectionSuffixLocal); }
public void RemoveTest () { builder = new SqlConnectionStringBuilder ("SERVER = localhost ;Network=DBMSSOCN"); // non existing key Assert.AreEqual (false, builder.Remove ("ABCD"), "#RT1 cannot remove non existant key"); Assert.AreEqual (true, builder.Remove ("NETWORK library"), "#RT2 should remove the key"); Assert.AreEqual ("Data Source=localhost", builder.ConnectionString, "#RT3 should have removed the key"); }
/// <summary> /// Creates a proxy server from the server in the given connection string using the default construction parameters and starts the proxy /// </summary> /// <param name="connectionString">Connection string to the server to proxy</param> /// <param name="newConnectionString">Connection string to the proxy server (using the same parameters as <paramref name="connectionString"/>)</param> /// <returns>The created and started proxy server</returns> public static ProxyServer CreateAndStartProxy(string connectionString, out string newConnectionString) { // Build builders SqlConnectionStringBuilder connStringbuilder = new SqlConnectionStringBuilder(connectionString); DataSourceBuilder dataSourceBuilder = new DataSourceBuilder(connStringbuilder.DataSource); // Setup proxy Task<System.Net.IPHostEntry> ipEntryTask = Dns.GetHostEntryAsync(dataSourceBuilder.ServerName); ipEntryTask.Wait(); System.Net.IPHostEntry serverIpEntry = ipEntryTask.Result; ProxyServer proxy = new ProxyServer(); proxy.RemoteEndpoint = new IPEndPoint(serverIpEntry.AddressList[0], dataSourceBuilder.Port ?? 1433); proxy.Start(); // Switch connection over dataSourceBuilder.Protocol = "tcp"; dataSourceBuilder.ServerName = "127.0.0.1"; dataSourceBuilder.Port = proxy.LocalPort; connStringbuilder.DataSource = dataSourceBuilder.ToString(); connStringbuilder.Remove("Network Library"); newConnectionString = connStringbuilder.ToString(); return proxy; }
internal static string RemoveDatabaseNameFromConnectionString(string connectionString) { //need to remove the actual database name so that it will not try and login to that individual database, but the whole DB server var builder = new SqlConnectionStringBuilder { ConnectionString = connectionString }; if (connectionString.ToLower().Contains("database")) { builder.Remove("Database"); } if (connectionString.ToLower().Contains("initial catalog")) { builder.Remove("initial catalog"); } return builder.ConnectionString; }
private void Save() { var sqlBuilder = new SqlConnectionStringBuilder(ConnectionString); sqlBuilder.DataSource = DataSource; sqlBuilder.InitialCatalog = InitialCatalog; sqlBuilder.IntegratedSecurity = IsIntegratedSecurity; if (!IsIntegratedSecurity) { sqlBuilder.UserID = UserId; sqlBuilder.Password = Password; } else { sqlBuilder.Remove("User Id"); sqlBuilder.Remove("Password"); } ConnectionString = sqlBuilder.ConnectionString; }
private static string FixUpConnectionString(string connectionString, string providerName) { DebugCheck.NotEmpty(providerName); if (providerName != "System.Data.SqlClient") { return connectionString; } var builder = new SqlConnectionStringBuilder(connectionString) { MultipleActiveResultSets = true }; builder.Remove("Pooling"); return builder.ToString(); }
static void EnsureDataBase(string connectionString) { var qs = new SqlConnectionStringBuilder(connectionString); var dbName = qs.InitialCatalog; qs.Remove("Initial Catalog"); using(var db = new SqlConnection(qs.ConnectionString)) { db.Open(); using(var cmd = new SqlCommand("select db_id(@db)" ,db)) { cmd.Parameters.AddWithValue("@db", dbName); if(cmd.ExecuteScalar() is DBNull) { cmd.CommandText = $"create database [{dbName}]"; cmd.ExecuteNonQuery(); } } } }
public static string GetSafeConnectionString(string connectionString){ var sb = new SqlConnectionStringBuilder(connectionString); sb.Remove("Password"); sb.Remove("User ID"); return sb.ToString(); }
private static string FixUpConnectionString(string connectionString, string providerName) { Contract.Requires(!string.IsNullOrWhiteSpace(providerName)); if (providerName != "System.Data.SqlClient") { return connectionString; } var builder = new SqlConnectionStringBuilder(connectionString) { MultipleActiveResultSets = true }; builder.Remove("Pooling"); return builder.ToString(); }
/// <summary> /// Removes the Initial Catalog setting from the connection string /// </summary> /// <param name="connectionString"> /// The connection string /// </param> /// <param name="initialCatalog"> /// The catalog to connect to /// </param> /// <returns> /// A connection string without the InitialCatalog /// </returns> private static string ConfigureConnection(string connectionString, string initialCatalog = null) { var builder = new SqlConnectionStringBuilder(connectionString); builder.Remove(InitialCatalog); if (!string.IsNullOrWhiteSpace(initialCatalog)) { builder.InitialCatalog = initialCatalog; } builder.Remove("Pooling"); builder.Pooling = false; return builder.ConnectionString; }
/// <summary> /// Determines if the database exists or not /// </summary> /// <param name="databaseName"> /// The database name. /// </param> /// <param name="connectionString"> /// The connection string. /// </param> /// <returns> /// true if the database exists /// </returns> public static bool InstanceStoreExists(string databaseName, string connectionString) { var builder = new SqlConnectionStringBuilder(connectionString); builder.Remove(InitialCatalog); using (var conn = new SqlConnection(builder.ConnectionString)) using (var command = new SqlCommand(DatabaseExistsSql, conn)) { conn.Open(); command.Parameters.AddWithValue(DatabaseNameParameter, databaseName); var result = command.ExecuteScalar(); return result != null && (int)result == 1; } }