Search code examples
c#sqltcpipsqlconnection

SQL cannot connect through ip address C#


I was wondering if someone could help me with this problem I have? I am not able to connect to my SQL server even though I correctly provided my IDE with the SQL server's port number and my IP address.

 static SqlConnection connection = new SqlConnection(@"Data Source=192.168.1.198,49172;Initial Catalog=irradix_base;Integrated Security=False;User ID=sa;Password=pass;");

Solution

  • So as it turns out, I found an answer from this post which uses a powershell command to find the listening port, here is the code:

    ForEach ($SQL_Proc in Get-Process | Select-Object -Property  ProcessName, Id | Where-Object {$_.ProcessName -like "*SQL*"})
    {
        Get-NetTCPConnection | `
         Where-Object {$_.OwningProcess -eq $SQL_Proc.id} | `
          Select-Object -Property `
                                    @{Label ="Process_Name" e={$SQL_Proc.ProcessName}}, `
                                    @{Label ="Local_Address";e={$_.LocalAddress + ":" + $_.LocalPort }},  `
                                    @{Label ="Remote_Address";e={$_.RemoteAddress + ":" + $_.RemotePort}}, State | `
          Format-Table
    }