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 (i.e. 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.
Related
I've been trying the following code to attempt to connect to a remote database I'm hosting on digital ocean.
MySqlConnection myConnection = new MySqlConnection("Server = 104.236.197.146; Port = 3306; Database = BattleRoyale; Uid = root; Pwd = password");
try
{
myConnection.Open();
}
catch (Exception e)
{
MessageBox.Show( e.ToString(), "Database connection error" , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Whenever I attemt to run this code I get the following error:
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySql hosts.
I believe the problem is with the hosted database. I've tried the following things:
GRANT ALL PRIVILEGES ON . TO root#"%" IDENTIFIED BY 'rootPass';
I've been trying to check to see if TCP/IP is enabled, but I cannot find it via phpMyAdmin nor going through putty or anything.
As you can tell, I don't know much about Linux machines and I'm just trying to connect to this database to work on a school project. Who knows what I've done trying to fix this issue. Any help would be greatly appreciated.
Possible reasons include:
MySQL is configured to listen on "localhost" or 127.0.0.1 only - this does not allow remote connections at all, regardless of the firewall configuration
Firewall may be configured to not let through communication on port 3306
MySQL may not be running on the standard port 3306 but some other port
Most probably it's 1. or 2. Especially 1. is something you don't think of at first.
Please note that many hosters don't allow access to the databases from outside their own network. That's why often PhpMySQL works but remote connections don't.
I've built an .asmx web service which retrieves informations from a local sql server 2014 database.
Everything is working fine on localhost, but after publishing the web service to Azure i get the error:
An unhandled exception of type 'System.Web.Services.Protocols.SoapException'
occurred in System.Web.Services.dll
Additional information: Server was unable to process request. ---> 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 Interfaces, error: 26 - Error Locating Server/Instance
Specified)
SQL Server is configured to allow remote connections.
My connection string is like:
string con2 = #"Data Source=OfficePc\MSSQLSERVER2014;Initial Catalog=Database;Persist Security Info=True;User ID=Admin;Password=123456";
Is the error the result of something missing from the connection string, or am i missing some configurations changes?
As Paul mentioned in a comment under the question, your connection string is pointing to a local database resource (presumably on your dev machine). Even though you configured your local database server to support remote connections, the address OfficePc\MSSQLSERVER2014 isn't addressable, as that does not equate to a machine address (IP address).
Your app would need to connect to your database via an accessible IP address (which might require you to do some port-forwarding on your local network, or open ports on your firewall).
Alternatively, you can migrate your database to Azure (either with SQL Server in a VM or with the SQL Database service).
Keep in mind: If you are accessing a local (on-premises) database server from Azure, there will be latency added, as well as some outbound bandwidth costs.
It looks like SQL Server instance is not running or not accessible. Try connecting to the same database using SSMS and if you get the same error then the instance is not running.
Mostly the error occurs when the Database server was not found. Recheck if the server name (Data Source) is mentioned correctly. If you manually generated the connection string use .uld file to generate connection string.
To auto generate connection string using .udl file:
Create a sampe.txt file.
Rename it as sample.udl file.
Then double click on it, It will show you window entitled 'Data Link Properties'.
Configure the connection there.
Then Test the connection using test connection button.
Then open the file with notepad. It will show you the exact connection string.
For further reference check : MSDN
I need to access database in another computer using c#...I am using connection string with IP address for accessing database and also changed firewall setting but it shows error as follows
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: TCP
Provider, error: 0 - A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.)
I m using this connection string
SqlConnection con = new SqlConnection("data source=192.168.1.12,1063;initial catalog=trinity;integrated security=false;network library=DBMSSOCN");
This is actually quite a large topic involving: Networking, Authentication, Authorization and Permissions. Quick checklist:
Open port 1433 on local firewall where SQL Server is running.
Open SQL Server Configuration Manager on the local machine where SQL Server is running and enable TCP connections.
Add a Windows Login or SQL Account user and grant permissions to the database. DON'T USE THE SA ACCOUNT TO CONNECT REMOTELY EVER!.
In your connection string you have Integrated security=false;, so you need to provide User ID=mysqlserveruserid;Password=mysqlserverpassword; parameters in your connection string instead. If you change Integrated security=true; your Windows credentials will be used.
Also why the IP Address? Do you not have network name resolution on your network? e.g. DNS or WINS.
Is SQL Server running as the default instance or does it have a named instance? If it's a named instance (would have been a choice taken when installing SQL Server) You will need Data source=computername\sqlserverinstancename;
More detailed information on all these aspects can be found on MSDN
https://msdn.microsoft.com/en-us/library/ms345332.aspx
Try given solutions, it may help you
http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx
When I try to connect to the MS SQL server in the local domain with SQL with SQL server authentication using the following code fails:
SqlConnection sql = new SqlConnection(conString);
sql.Open();
sql.Close();
I can connect to my local SQLExpress test database just fine, using either windows or sql authentication. I tried using many different connections strings for the domain database, including using the connection string generated by (successfully) adding the server as a DataSource. What am I doing wrong?
EDIT: When I add the server in the server explorer of visual studio, I can connect successfully. I would assume that the connection string thereby generated is valid:
Data Source=mySubDomain.myDomain.local;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myUser;Password=myPassword;
I checked and remote connections are allowed. I do not have permission to access the windows server which the sql server is running on, so I can't check any further settings.
EDIT 2: The following message comes with the exception:
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 Interfaces, error: 26 - Error Locating Server/Instance Specified)
EDIT 3:
I tested my application on a different computer in the same domain, and it worked. Does someone have a clue what's going on or how I could find out?
The Group Policy or firewall (or both, not sure) of this company domain make my .exe file (and any other .exe) behave in the following way:
Launching it from C:\Program Files (or C:\Program Files (x86)) as any user works fine
Launching it from anywhere else as a normal user is not allowed and fails
Launching it from anywhere else as a local admin will execute the application, but connections are blocked
I Get this error message whenever I tried to up my aspx page.
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
whic is connected in this connection string
SqlConnection conn = new SqlConnection("Data Source=192.168.xxx.xxx;Initial Catalog=DBSample;User ID=dev;Password=pass;Integrated Security=SSPI;"))
The weird thing is that the server that I'm connecting has already hosting some aspx page. I don't knnow if there's missing in my connectiong string Thanks. and I know the server that I'm connecting to is already allowed remote connection since it's already hosted some aspx websites. :(
Thanks!
Do you need to add an instance name to your connection string? Do you have the SQLBrowser service running on the target machine, or do you have to specify a port for the instance?
You also get that very same error when the database doesn't exist at the location that you are trying to connect to. Have tried looking at the connection strings of the aspx pages that are successfully connecting?
edited: Specifying Integrated Security=SSPI means you will be using Windows authentication to login to the database. What user is your aspx page running as (check your app pool)? Does it have the rights to log in to the database? This could also explain why it works on one server but not another.
Are you trying to connect to a hosted SQL Server over TCP/IP?
The reason I ask is some firewalls block traffic over Port 1433.
If not then it is simply a case of validating the connection string details and ensuring the SQL Server Engine is actually running...
Have you EVER been able to connect to this database from the PC you are currently attempting this connection on?