/// <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; }
private static void PrepareConnectionStrings(string originalString, out string tcpString, out string tcpMarsString, out string npString, out string npMarsString) { SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder(originalString); DataSourceBuilder sourceBuilder = new DataSourceBuilder(connBuilder.DataSource); sourceBuilder.Protocol = null; // TCP connBuilder.DataSource = sourceBuilder.ToString(); connBuilder.MultipleActiveResultSets = false; tcpString = connBuilder.ConnectionString; // TCP + MARS connBuilder.MultipleActiveResultSets = true; tcpMarsString = connBuilder.ConnectionString; // Named Pipes sourceBuilder.Port = null; connBuilder.DataSource = "np:" + sourceBuilder.ToString(); connBuilder.MultipleActiveResultSets = false; npString = connBuilder.ConnectionString; // Named Pipes + MARS connBuilder.MultipleActiveResultSets = true; npMarsString = connBuilder.ConnectionString; }