Connection String to MySQL Database on LAN Setup - c#

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.

Related

How to Connect SQL server from client to Server PC using Connection string in C#

I am trying to connect SQL Server from Client PC using the below-mentioned string, but it Is showing error as "Login failed for user 'sa'"
Tried string:
public static string connString = #"Data Source=(eg.IP)10.0.255.255,1433; Network Library=DBMSSOCN;Initial Catalog=InventoryProjects;User Id=sa;Password=password";
I have provided IP , userid and passward are correct.
Happening:
DB is not connected from Client PC.
Expectation:
But it should connect as I expected.
Can Anyone Please guiding me to proceed Further, because I am not very much familiar in C# and SQL
Points that spring to mind:
(eg.IP) should be removed from the connection string
Is IP address correct - may not be if it is dynamic?
Is the user id and password correct - use of sa user id may not be allowed?
Check - if you are able to run Microsoft SQL Server Management Studio to connect to the SQL database and check the connection string.

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.

Can't connect to SQL Server Express via IP address - 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.

SQL connection string on Windows Server 2008

I need to connect my C# desktop app on PC 1 with database that exist on SQL server instance at Windows Server 2008. First I cannot make my connection string works.
My connection string is:
Server=(Server_Ip)192.168.1.115\(InstanceName)SQLExpress8;initial
catalog=My_Database;integrated security=True;MultipleActiveResultSets=True;
I also tried:
Server=(ServerName)DATABASE\(InstanceName)SQLExpress8;initial
catalog=My_Database;integrated security=True;MultipleActiveResultSets=True;
But that doesn't work.
By the way I have replaced server with datasource and it still doesn't work.
I am not sure if you are putting the parenthesis in your connection string but this is an example of a working one for sql server
connectionString="Server=testServer\instanceName; uid=readOnlyUser; pwd=1234567; database=testDatabase" providerName="System.Data.SqlClient"
where
testServer = IP or server name
instanceName = instance name, sometimes SQLExpress for sql server express
testDatabase = database name
uid = user name
pwd = password
you CAN connect your windows app to the database, however the server IP has to be public to the internet, you will have to mess with firewall settings in order to do that to allow the IP to be accessed publicly.

How to address a remote MySQL server in the connection string?

I use MySQL .NET Connector to communicate with the mysql server. And everything is fine if I set the connection string as:
<add key="ConnectionString" value="Server = localhost; Port = 3306; Database = test; Uid = root; Pwd = root;" />
I'm going to use my program to reach the MySQL Server remotely from another location. How will the connection string supposed to be set in this case?
I tried:
... Server = http://localhost; Port = ...
just as experimenting, but didn't work. Thank you.
Server = localhost specifies the name of the server connected to. In this case it refers to localhost a special name which always refers to the local computer regardless what it is actually called.
You can change this value to the NETBIOS name, DNS name or IP address of the server.

Categories

Resources