Access Wamp Mysql from extenral application - c#

I have this code snippet where I am trying to make a connection with a remote database, located on LAN Ip with wamp.
string myConnectionString = "server=192.9.210.62;port=8088;database=testDB;uid=root;pwd=;";
MySqlConnection con = new MySqlConnection(myConnectionString);
con.Open();
MessageBox.Show("connection open");
con.Close();
But it is failing due to socket exception. here are the error
MySql.Data.MySqlClient.MySqlException: 'Reading from the stream has
failed.'
Then, I search for this problem, and it seems that I have to allow a remote connection to my SQL. But remember I have already put my server online already. There is also bind-address setting that need to be set 0.0.0.0 but i am unable to find it in .ini file.

Related

:'Unable to connect to any of the specified MySQL hosts.' error

I want to connect to a MySQl db using a connection string but unable to connect
I'm using UWP and using The fall creator version for both target and min version
I've installed the Nuget packages for Mysql connector
I've tried to change the string with and without port,ect.
public string GetMatchCode()
{
string connectString = "Server=###.###.###.###;Database=Db;Uid=root;Pwd=123;sslmode=none;port=3306";
string sql = "SELECT * FROM `customer`";
using (var connect = new MySqlConnection(connectString))
using (var command = new MySqlCommand(sql, connect))
{
connect.Open();
return command.ExecuteScalar().ToString();
}
}
MySql.Data.MySqlClient.MySqlException: 'Unable to connect to any of the specified MySQL hosts.'
MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The error starts at connect.Open();
I've tested the connection with workbench to make sure it's working.
I've looked through most similar post but none have worked for me.
There could many possible scenario's where connection will fail.
Please see below points to troubleshoot your error.
Check your Windows Firewall to ensure that server has an access to Port's associated with your MySql.
Disconnect and re-connect your VPN client.
Follow standard Spacing and Order of parameters in connection string. Follow below format to build/form connection string.
Server=ServerAddress; Port=4563; Database=DataBaseName; Uid=UserName;
Pwd=Password;
Consider using Connection Pulling, So existing connection will be reused.

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.

Sql connection string via SqlClient

I have an issue trying to manually connect to a sql server on my network.
I have a web API with a conn string that works fine, I have a VS2015 winforms project i set up to test the conn using the VS tool to connect, this also works fine.
However when i try to connect via SQLconnection it fails and says the server cannot be found or isnt accessable.
So heres the test code...
string connectionString = #"Data Source=test1\test1;Initial Catalog=my_test;Integrated Security=True;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
// Create the Command and Parameter objects.
SqlCommand command = new SqlCommand("", connection);
// Open the connection in a try/catch block.
// Create and execute the DataReader, writing the result
// set to the console window.
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}\t{2}",
reader[0], reader[1], reader[2]);
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
Now i dont really care about results or anything i simply need to establish a connection. I cant figure out why it throws this error, when my Web API works and the VS tool for connection to DBs works fine too.
I have checked the server and enabled named pipes too just to make sure.
Any ideas?
EDIT:The error i catch...
{"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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}
happens after about 15 seconds trying to open the connection.
The thing is this connectiin string i tried is the exact same string used via the SQL tool, I did this so i knew i had a valid string.
ODBC connection also falls over with the same message.
I checked from my machinbe is i can listen to port 1433 but it says the server isnt listening.
However looking at the server its set to dynamic ports with a range of any to 41934, so im at a loss as to why it says its not listening evenb though its to dynamic ports
EDIT2: Well, must be permissions or drivers... just tried the exact same code and worked instantly...
This is diving into the realm of the unknown i feel. might ask for deletion of the question if no solution is found
I think that i see the problem:
string connectionString = #"Data Source=test1\test1;Initial Catalog=my_test;Integrated Security=True;";
Integrated Security is used for local logins, You could make an account in the database and change the connection string to:
string connectionString = #"Data Source=test1\test1;Initial Catalog=my_test;User id=<Username>;Password=<Password>;";
I hope this helps!

Can't create connection to an sqlite DB

I've made a code that create the database automatically in the folder where the software is executed. The DB was created succesfull, but when I create the object for the connection this exception is displayed:
System.Data.SqlClient.SqlException (0x80131904): There was a network error or specific instance while trying to establish 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): Can not find the file specified
on this line:
con.Open();
but the problem I think is on this declaration of the connection:
static SqlConnection con = new SqlConnection(
"Server=localhost;Database=AppDB.sqlite;");
What am I doing wrong?
UPDATE Details:
SqlCommand command = new SqlCommand(#"INSERT INTO Team (name,code, shortName, squadMarketValue,
crestUrl, link_self, link_fixtures, link_players, caption) VALUES (#name,
#code,#shortName,#squadMarketValue,#crestUrl,#link_self,#link_fixtures,
#link_players,#caption)", con);
How you can see I'm using parametizer query, and if I use this object:
static SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=SoccerForecast.sqlite;Version=3;");
the con variable is underlined in red, an the compilers tell me:
it is not possible to convert SqliteConnection in SqlConnection
Your connection is wrong. You didn't specified the kind of database you're trying to connect to in your question. There's a mention of SQLite while your are using an sql connection object. The sql connection object is for connection to the ms sql server database. If you really want to connect to an ms sql server database, you'll need to change the connection string to provide a username / password or a Trusted_Connection=True to use your windows account.
However, if the database you are trying to connect to is an SQLite DB, you will have to use a different connection object. This and this SO question has answers about available libraries and examples that will explain how to create a connection to a SQLite database.
The site connectionstring.com is also a good resource when you need to generate a connection string.

Can not connect to secure phpmyadmin through my c# code

I can not open a new connection through my code to my digitalocean server.. It's my first account and will be my first online database program.. here's what I did
after creating an account on digitalocean I've created a droplet and installed LAMP and secure phpmyadmin by following the installation tutorials..
I've given an IPv4 Address: xx.xxx.xx.xxx
when I typein xx.xxx.xx.xxx/phpmyadmin to my browser it asks for username and password, than comes the phpmyadmin screen which asks for username root and it's password
I've created a new database= TestApp and users table..
and then I made a very basic program via c#, I have a textbox and a button and the code is as follows:
string conString = "Server=xxx.xxx.xxx.xx;Database=TestApp;Uid=root;pwd=_______";
MySqlConnection newCon = new MySqlConnection(conString);
string insertCommand = "INSERT INTO TestApp.users(isim) VALUES (?isim)";
MySqlCommand cmd = new MySqlCommand(insertCommand, newCon);
newCon.Open();
cmd.Parameters.Add("?isim", txtpost.Text);
cmd.ExecuteNonQuery();
newCon.Close();
when I run the program it gives the following error:
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Additional information: Unable to connect to any of the specified MySQL hosts.
how do I solve my issue?
I don't know DigitalOcean, but I am assuming that the virual server has been configured to only allow web traffic into it (i.e. ports 80 and 443, which is why phpmyadmin works).
Your C# code (is it's not on the server itself) will need to access the MySQL port which by default is 3306. You will need to find instructions on DigitalOcean for opening this up.

Categories

Resources