/// <summary> /// Establish connection and log into the SQL Server /// </summary> public void Connect() { // Initialize context TDSClient.OnPreConnect(); // Loop while we reach logged-in state. This accounts for connection failures and routing. while (TDSClient.State != TDSClientState.LoggedIn) { Log("Connecting to the server {0} port {1}...", TDSClient.Context.ServerHost, TDSClient.Context.ServerPort); // Check if server pipe is specified if (string.IsNullOrEmpty(TDSClient.Context.ServerPipe)) { try { // Establish transport to the TDS Server ClientSocket = new TcpClient(AddressFamily.InterNetwork); ClientSocket.Connect(TDSClient.Context.ServerHost, (int)TDSClient.Context.ServerPort); } catch (SocketException e) { // Check error code if (e.ErrorCode != 10057) { // We don't recognize it throw; } // We are going to retry with IPv6 now because of won't fix bug // http://bugcheck/bugs/VSWhidbey/285220 ClientSocket = new TcpClient(AddressFamily.InterNetworkV6); ClientSocket.Connect(TDSClient.Context.ServerHost, (int)TDSClient.Context.ServerPort); } // Callback of PostConnect if (_funcPostConnect != null) { _funcPostConnect(ClientSocket); } } else { // Use named pipes transport ClientPipe = new NamedPipeClientStream(TDSClient.Context.ServerHost, TDSClient.Context.ServerPipe, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation); ClientPipe.Connect(); } Log("Connected"); try { // Check if we have a client socket if (ClientSocket != null) { // Create a client TDS parser with TCP transport ClientParser = new TDSClientParser(TDSClient, ClientSocket.GetStream()); } else { // Create a client TDS parser through named pipes transort ClientParser = new TDSClientParser(TDSClient, ClientPipe); } if (_funcTDSStreamPreWriteCallBack != null) { ClientParser.SetTDSStreamPreWriteCallback(_funcTDSStreamPreWriteCallBack); } // Assign event log ClientParser.EventLog = EventLog; // Run login sequence ClientParser.Login(); // Check if connection is being re-routed if (TDSClient.State == TDSClientState.ReConnect) { Log("Client is being routed"); // Close established connection Disconnect(); } } catch (Exception) { // Disconnect client Disconnect(); // Bubble up the exception throw; } } }
/// <summary> /// Establish connection and log into the SQL Server /// </summary> public void Connect() { // Initialize context TDSClient.OnPreConnect(); // Loop while we reach logged-in state. This accounts for connection failures and routing. while (TDSClient.State != TDSClientState.LoggedIn) { Log("Connecting to the server {0} port {1}...", TDSClient.Context.ServerHost, TDSClient.Context.ServerPort); // Check if server pipe is specified if (string.IsNullOrEmpty(TDSClient.Context.ServerPipe)) { try { // Establish transport to the TDS Server ClientSocket = new TcpClient(AddressFamily.InterNetwork); ClientSocket.Connect(TDSClient.Context.ServerHost, (int)TDSClient.Context.ServerPort); } catch (SocketException e) { // Check error code if (e.ErrorCode != 10057) { // We don't recognize it throw e; } // We are going to retry with IPv6 now because of won't fix bug // http://bugcheck/bugs/VSWhidbey/285220 ClientSocket = new TcpClient(AddressFamily.InterNetworkV6); ClientSocket.Connect(TDSClient.Context.ServerHost, (int)TDSClient.Context.ServerPort); } // Callback of PostConnect if (_funcPostConnect != null) { _funcPostConnect(ClientSocket); } } else { // Use named pipes transport ClientPipe = new NamedPipeClientStream(TDSClient.Context.ServerHost, TDSClient.Context.ServerPipe, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation); ClientPipe.Connect(); } Log("Connected"); try { // Check if we have a client socket if (ClientSocket != null) { // Create a client TDS parser with TCP transport ClientParser = new TDSClientParser(TDSClient, ClientSocket.GetStream()); } else { // Create a client TDS parser through named pipes transort ClientParser = new TDSClientParser(TDSClient, ClientPipe); } if (_funcTDSStreamPreWriteCallBack != null) { ClientParser.SetTDSStreamPreWriteCallback(_funcTDSStreamPreWriteCallBack); } // Assign event log ClientParser.EventLog = EventLog; // Run login sequence ClientParser.Login(); // Check if connection is being re-routed if (TDSClient.State == TDSClientState.ReConnect) { Log("Client is being routed"); // Close established connection Disconnect(); } } catch (Exception) { // Disconnect client Disconnect(); // Bubble up the exception throw; } } }