DEV Community

FakeStandard
FakeStandard

Posted on • Edited on

How to Quickly Test SQL Server Connection from a Client

If you have a SQL Server environment and want to confirm if your client machines can connect to the database, how do you actually verify the connection is working?

Here's a quick rundown of the common ways I use every time I check a connection.

1️⃣ Use sqlcmd CLI Tool to Test Connection

This is probably the fastest way to test a SQL Server connection using the sqlcmd command line tool.

sqlcmd -S YourServerIP,1433 -U UserName -P Password
Enter fullscreen mode Exit fullscreen mode

If the connection fails, it will show you the error details.

2️⃣ Test TCP/IP Connectivity with PowerShell

Sometimes the problem might be network-related, like firewall or port blocking. To verify if your client can reach the SQL Server's TCP port, use the following command in PowerShell.

Test-NetConnection -ComputerName yourserverip -Port 1433
Enter fullscreen mode Exit fullscreen mode

If TcpTestSucceeded returns True, it means your client can communicate through port 1433 to the SQL Server.

3️⃣ Create a Console App to Test Connection

When you want to verify the connection from code, here's a minimal .NET 8 console app example using a connection string.

try
{
    using SqlConnection conn = new(connectionString);
    conn.Open();

    Console.WriteLine("Connection successful");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
Enter fullscreen mode Exit fullscreen mode

If you still can't connect to SQL Server, I suggest checking out the previous article in this series.

Easy fix. Job done☑️


Thanks for reading!

If you like this article, please don't hesitate to click the heart button ❤️
or follow my GitHub I'd appreciate it.

Top comments (0)