I’m working on a small app to connect to SQL Server, show all databases, and run some scripts on the chosen one. But I’m hitting a snag.
Here’s my code to get the databases:
public DataTable FetchDatabaseList(string serverIp, bool useWindowsAuth, string username = null, string password = null)
{
string connStr = useWindowsAuth
? $"Data Source={serverIp},1433;Integrated Security=True;"
: $"Data Source={serverIp},1433;User Id={username};Password={password};";
try
{
using (var conn = new SqlConnection(connStr))
{
conn.Open();
return conn.GetSchema("Databases");
}
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
return null;
}
}
I’m using the IP address (like 192.168.1.100) instead of the server name. No specific instance mentioned.
When I try this on different servers, I get errors like:
- “Can’t use Windows auth from untrusted domain”
- “Can’t find or access the server”
Any ideas what’s going wrong? I thought using the IP would make it work everywhere.
have u tried using the server name instead of IP? sometimes that works better. also, whats ur firewall situation? maybe its blocking the connection. oh, and for windows auth, r both machines in the same domain? that could be why it’s not working. just curious, have u checked if SQL Browser service is running on the server?
Using an IP address for the connection can introduce several challenges that are not immediately obvious from the code. In my experience, remote connections require a properly configured SQL Server instance where remote connectivity is enabled, and the appropriate ports are allowed through the network firewall. Windows authentication further complicates matters because both the client and server need to have a shared domain trust or be part of the same domain. It is also important to ensure that the SQL Server Browser service is running if an instance name is not explicitly specified. Making these configuration adjustments and verifying them using SQL Server Configuration Manager has helped resolve similar issues.
ip addresses can be tricky. ensure remote access is enabled, the firewall isn’t blocking port 1433, and both machines share a domain for windows auth. also, check that sql browser is running, or try the server name.