简体   繁体   中英

.NET C# - Cannot access a SQL Server database behind remote machine

I have the credentials to access a server in a local network - a network that is behind a proxy. And that server is running a SQL Server 7.0 database. The database is configured to use Windows authentication for log in.

I use the Remote Desktop Connection to access the server, put in the credentials, and when inside I open the Query Analyser, select log in with Windows authentication and then I query the DB.

But, now I need a .NET C# program to access the database from my machine, remotely. I have checked for a TCP/IP connection on port 1433 and it's open. However, with the following code, I cannot open a connection to the remote database.

SqlConnectionStringBuilder connStringBuilder = new SqlConnectionStringBuilder();
connStringBuilder["Trusted_Connection"] = false;
connStringBuilder["user id"] = "<domain>\\<user>";
connStringBuilder["password"] = "<pass>";
connStringBuilder["Database"] = "<db>";
connStringBuilder["Server"] = "\\\\<servername>\\MSSQLServer,1433";
connStringBuilder["Connection Timeout"] = "30";

SqlConnection myConn = new SqlConnection(connStringBuilder.ConnectionString);
try
{
    myConn.Open();
    // success
    Console.WriteLine("Connection Opened");
}
catch (Exception e)
{
    // failed
    Console.WriteLine("Connection Failed");
    Console.WriteLine(e.ToString());
}

The servername field is the same text I put in the Remote Desktop Connection tool, as it is with the user id and password fields after a connection is established (normal log on with Windows Server 2000).

Also, with the SSMS (SQL Server Management Studio) I cannot connect to the instance, however with Windows Explorer I can browse the server's drives with \\\\servername\\e$ (for example).

Thank you in advance!

EDIT 1

I believe the problem is go through the remote machine log in, but I have no idea how to do that. I tested on a local db on my machine and the program works like a charm.

The error message from the exception e is the following, weather with true or false on the Trusted Connection:

System.Data.SqlClient.SqlException: A network-related or instance-specific error
occurred while establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that SQL Server is
configured to allow remote connections. (provider: SQL Network Interface, error: 25 -
Connection string is not valid)
at .... etc

EDIT 2

The steps I take to log on to the machine using some credentials are the following:

I first put in the server name as in "MACHINE33" (without quotes)

Remote Desktop Connection

And then I put the credentials \\ as in "me\\johndoe"

Credentials to access the remote machine

In the end the database is accessed using the windows authentication used to access the machine.

If you are using integrated authentication then the value for trusted connection should be either 'true' or 'sspi', not false. If 'true', any user id and password you specify on the connection string will be ignored so might as well not set them if you are intending to use the credentials for the currently logged in user (or the AppPool user if a web app, or the service user if a windows service etc.).

Are you using a named SQL Server instance? If so, a dynamic port is used, not 1433. You would need to either configure SQL Server to use a specific port for the named instance, or not specify a port.

"Login failed for user" when specifying default port

I have asked the systems administrator, this morning, and he told me that the database cannot be accessed from the outside , even though there is a TCP/IP configuration on port 1433 (checked under Server Network Utility for SQL Server 7.0). The reason is that the whole network is behind a proxy that checks all incoming requests and prevents the ones not coming from the tools specified (ie Remote Desktop Connection).

Since what I want is to migrate some fields from the structure of the old database to a new one, used by a new system, the solution is:

Access the remote machine, create a backup of the database, access the files through mapping the network drive, copy the backup files to your machine, create a local database and restore the backup to your new local database.

It's not the most elegant way, but it's the only way. And as an advice, first ask the systems administrator if you're behind a network full of proxies because it can save you a lot of headaches.

Thank you and sorry for the whole trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM