Can't connect to SQL Server Express via IP address - c# - c#

I moved from SQL Server 2014 to SQL Server Express (free version) and since then I can't connect to database using SqlConnection with IP.
I can connect local with connection string but when I am using an ip address I get error number 10061, I checked whole net about that error and verified all the issues mentioned: remote connection is allowed, I added port in firewall, enabled tcp in server...
In official version of SQL Server it works fine, but when I moved to EXPRESS free version I can't successfully connect remotely any more.
This is the connection string I use:
SqlConnection cn = new SqlConnection(#"user id = ***; password=***;server=192.168.1.102,1433; Trusted_Connection=no; database=myDatabase; connection timeout=30");
Getting exception error number 10061.
But when I use the local connection string:
SqlConnection cn = new SqlConnection(#"server=ASUS\SQLEXPRESS ;database=myDatabase;user id = ***; password=***;");
That works fine...
What can the problem be?

You still need to specify the instance name when switching to using the I.P to connect.
SqlConnection cn = new SqlConnection(#"user id = ***;
password=***;
server=192.168.1.102\SQLEXPRESS;
Trusted_Connection=no;
database=myDatabase; connection timeout=30");

I should have set the port and ip in server configuration manager.

Related

Connecting to a remote SQL Server (SQLExpress) gives "network-related" error, but works using Server Explorer in Visual Studio

I am using NET 6.0 and I cannot make it work.
I am trying to connect using:
var builder = new System.Data.SqlClient.SqlConnectionStringBuilder
{
DataSource = $"{ip}\\SQLEXPRESS, 1433",
InitialCatalog = "xxx",
UserID = "user",
Password = "pass"
};
SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = builder.ConnectionString;
thisConnection.Open();
However, I keep getting this
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 - No connection could be made because the target machine actively refused it.)
Using exactly the same code on Netframework 4.8 works. Anybody care to explain why? Using the same machine as above, if I use the Server Explorer in Visual Studio then the connection is successful. Also works using Python. I tried ussing the connection string from the Server Explorer connection string and still doesn't work. The connection string from that works using the Server Explorer:
Data Source=xxx.xxx.xx.xx\sqlexpress;Initial Catalog=database;Persist Security Info=True;User ID=sysdba;Password=****
Here is the Server Explored connected:
And the properties:
And then:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = #"Data Source = xxx.xxx.xx.xx\sqlexpress; Initial Catalog = database; Persist Security Info = True; User ID = sysdba; Password = ******";
conn.Open();
But still fails to connect.
I also set the port to 1433 like suggested in Cannot Connect to Server - A network-related or instance-specific error and restarted the service
Also
And still fails to connect using the SqlConnection() (still works using the Server Explorer).
What am I doing wrong?
The SQL Server uses SQL Server Authentication, no Windows Credentials. I connect using IP\sqlexpress.
It doesn't seem to be a network related issue, like I saw in other answered questions, since it is working using the Server Explorer in Visual Studio and also in Python.
I was facing the same issue (Net 5.0). You can use the Connnected Services, which is a collection of tools in Visual Studio that will help you connect to the database. I believe this may be a different approach, but it should work since visual studio will be creating the connection string (same was as per Server Explorer).
https://learn.microsoft.com/en-us/visualstudio/data-tools/add-new-connections?view=vs-2022
https://learn.microsoft.com/en-us/visualstudio/azure/overview-connected-services?view=vs-2022

connection to MySql database on host

I have a problem regarding connecting to my database that is located on my website host server. I have watched few tutorials, took a look on few articles here on stack overflow and read official mysql documentation, and i steel can't connect to it thru my c# console app. This is my code:
string connstring = string.Format("Server=www.vm-consult.com; Database=vmconsul_sitedatabase; Uid=vmconsul_mijovicpetar; Pwd=mypassword");
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = connstring;
connection.Open();
I have also went to c panel in remote MySQL tab and set "%" to my remote sql hosts.
All the parameters for con string are correct. In server in connection string I also tried to set IP address, did not change anything.
This is exception I get(on the last line: connection.open()):
Unable to connect to any of the specified MySQL hosts.
Any suggestions appreciated. Thanks.
Check connectivity with
telnet www.vm-consult.com 3306
I needed to contact my host provider to enable external connection to the database. Thanks everybody.

OleDBConnection error Database Server Not Found C#

I'm trying to connect to a remote SQL Anywhere 10 database server using the following connection string but receiving a "Database Server Not Found" error.
OleDbConnection conn = new OleDbConnection("Provider=SAOLEDB.10;ENG=dental;DBN=dentserv;
LINKS=tcpip(host=192.168.1.100,PORT=2638);
Integrated Security = True; User ID = dba; PWD = sql");
Using the same code, I can connect to a local database that is setup the same way as the remote server I'm attempting to connect to without any issue:
OleDbConnection conn = new OleDbConnection("Provider=SAOLEDB.10;Data Source = dental;
Database=dental;Integrated Security = True; User ID = dba; PWD = sql");
I've added and removed multiple variables within the string to troubleshoot to see if there was anything missing (ENG instead of Data Source, with and without DBN, DSN, etc.), most seem to be interchangeable but always throwing the same error of no server.
For some additional information, I am able to connect to this server database using a different database management tool using the same port IP credentials with no issue. It's Database URL is: jdbc:sybase:Tds:192.168.1.100:2638?ServiceName=
Connection String:
DBN=DENTSERV;DSN=DENTAL;UID=DBA;PWD=SQL
Any thoughts? I've been working on this for awhile with no further progress. Any insight will be much appreciated!
The host (host=192.168.1.100); appears to be a private IP (IP for your local network), you cant use that for remote access you may use a Public IP also the server need to be configured for remote access.

Connection String to MySQL Database on LAN Setup

With the service running on my machine(Setup on a LAN with no internet connection), I can connect with the following :
string sConnection = #"Server=localhost; Port=3306; Database=some_database; Uid=root; Pwd=genericpassword;";
MySqlConnection cnTest = new MySqlConnection(sConnection);
cnTest.Open();
How should I Modify my connection string to connect from another PC on the lan to my MYSQL server?
*side note : The IP of my local machine is setup to be 192.168.0.1.
What goes after "Server=" is the address of the server.
So if your server is on 192.168.0.1 - it'll look like
string sConnection = #"Server=192.168.0.1; Port=3306; Database=some_database; Uid=root; Pwd=genericpassword;";
Just replace 'localhost' with the address you're given.
That said, it's generally considered to be good procedure to store connection strings in web.config / app.config - whenever you deploy it on some other environment, you won't want to rebuild it. This link might help with that.

Can't connect to SQL Server 2005 Express from an ASP.NET C# page

I have an MS SQL Server 2005 Express running on a VPS.
I'm using pymssql in Python to connect to my server with the following code:
conn = pymssql.connect(host='host:port', user='me', password='pwd', database='db')
and it works perfectly.
When I try to connect to the server from an ASP.NET C# page with the following code:
SqlConnection myConnection = new SqlConnection("Data Source=host,port;Network Library=DBMSSOCN; Initial Catalog=db;User ID=me;Password=pwd;");
myConnection.Open();
When I run the ASP.NET page I get the following exception at myConnection.Open();:
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 tried restarting the SQL Server but I had no luck.
Can anyone point me out to what I'm missing here?
Thanks!
Let's say your host is localhost. When you are using SQLEXPRESS you need to specify that in your Instance Name.
Loke this: Data Source=localhost\SQLEXPRESS if it is bound to localhost. Otherwise it might just work with: Data Source=.\SQLEXPRESS.
If you have management studio installed you can fire that up and check what connection string it is using!
Still doesn't work..
I'm able to connect to the remote server with SQL Management Studio.
I enter host,port\SQLEXPRESS (of course I my actual IP number as the host and my actual port) in the Server Name field, select SQL Security and enter my username and password and it works perfectly.
When I try to connect from the ASP.NET page, it just doesn't work - I get the error aforementioned. Can it have something to do with the company hosting my asp.net page? (GoDaddy)
here is the code again..(assuming my host is 11.22.33.44 and my db is named bla
string connectString = #"Data Source=11.22.33.44,1433\SQLEXPRESS;Network Library=DBMSSOCN;Initial Catalog=bla;User ID=username;Password=pwd;";
SqlConnection myConnection = new SqlConnection(connectString);
myConnection.Open();
Thanks again

Categories

Resources