I'm trying to connect to my mysql database (I'm hosted by infinityfree.net), entering these credentials:
connection = new MySqlConnection("Server=sql306.epizy.com; Port=3306; Database=epiz_27674946_db1; Uid=epiz_27674946; Pwd=**********; ");
But I get this exception:
Unable to connect to any of the specified mysql hosts
I don't know why this happens, the credentials should be right...
Can you help me?
Full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace sharetru
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MySqlConnection connection;
private void Form1_Load(object sender, EventArgs e)
{
try
{
connection = new MySqlConnection("Server=sql306.epizy.com; Port=3306; Database=epiz_27674946_db1; Uid=epiz_27674946; Pwd=*******; ");
connection.Open();
if (connection.State == ConnectionState.Open)
{
label1.Text = "Connected";
label1.ForeColor = Color.Green;
}
else
{
label1.Text = "Not Connected";
label1.ForeColor = Color.Red;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Regarding this service (infinityfree) it is not possible to access mysql from desktop applications (outside the infinityfree host)
https://support.infinityfree.net/mysql/how-to-connect-with-mysql/
The code looks correct. The issue probably is in the connection string details.
Are you able to connect using the following connection details via some GUI tool?
Workbench for example https://dev.mysql.com/downloads/workbench/
Related
I've just working with c# and mysql and have ran into a problem. I've created a mysql db(remote access) using mysql workbench. I've been using visual studio to write the code(c#), xamarin.forms. The aim is to connect the database on multiple platforms. I'm trying to make it connect through windows(uwp) and android, it works fine on the windows(uwp). However when I try and run it through my android device to test the app I get an error. The error states "SYSTEM.TYPEINITIALIZATIONEXCEPTION......OPERATION IS NOT SUPPORTED ON THIS PLATFORM". Any idea on how to get around this? Thank you.
This is the code:
'''
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using MySql.Data.MySqlClient;
using System.Data;
namespace demo {
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
void Button_Clicked(object sender, EventArgs a)
{
MySqlConnection con = new MySqlConnection(user id; password);
try
{
con.Open();
if (con.State == ConnectionState.Open)
{
((Button)sender).Text = $"Connected";
}
}
catch (Exception ex)
{
((Button)sender).Text = ex.ToString();
}
con.Close();
}
}
}
I am trying to create a project that will read, write,and check for duplicates in my access data base file. I am using C# and keep getting "Connection failed error that I have written into the program if the connection state = 0. If anyone can offer any help I would appreciate it. I am using Access 2016 and I am not sure what references I need for my project (if any). Everything I have found online is either outdated or doesn't work.
Thank you!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Data.OleDb;
namespace RHG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (OleDbConnection connection = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\grosales\Documents\rhg\RHG\Used.accdb"))
{
try
{
connection.Open();
MessageBox.Show("connected");
}
catch (Exception ex)
{
MessageBox.Show("connection failed");
}
}
}
`
You have not opened the connection.
connection.Open();
Note: Checking the connection state is not enough. You might get an exception when trying to open the connection. Enclose it in try-catch instead.
It is also a good practice to enclose work on a connection with using
using (var connection = new OleDbConnection()) {
connection.ConnectionString =
#"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\...\Used.accdb";
try {
connection.Open();
//TODO: do something with the connection
} catch (Exception ex) {
MessageBox.Show("Connection Failed\r\n\r\n" + ex.Message);
}
}
This ensures that the connection will be closed and the resources be released.
Try following the examples here: https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.110).aspx
You're missing the connection.Open(); statement, which should be wrapped in a try catch.
Additionally, you can specify the connection string within the constructor, like
using (OleDbConnection connection = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.16.0;Data Source=C:\Users\grosales\Documents\rhg\RHG\Used.accdb"))
{
//do DB access here
}
//no need to call connection.Close() - it's automatically done once you leave the using block.
The following code checks if I can connect to the MySQL database or not. It is working fine when it connects but when it can't connect it throws out an error instead of executing the 'Else' statement
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySQLConnection
{
public partial class FrmConnection : Form
{
public FrmConnection()
{
InitializeComponent();
}
string connectionString = "host=192.168.0.91; database=c#1; user=test1; password=test1";
private void button1_Click(object sender, EventArgs e)
{
using (MySqlConnection con= new MySqlConnection(connectionString))
{
con.Open();
if(con.State==ConnectionState.Open)
{
label1.Text = "Connection Established!";
}
else
{
label1.Text = "Connection Error!";
}
}
}
}
}
I get this 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.
To handle exceptions, you can do this:
using (MySqlConnection con= new MySqlConnection(connectionString))
{
try
{
con.Open();
label1.Text = "Connection Established!";
}
catch(Exception ex)
{
label1.Text = "Connection Error!\n"+ex.Message;
}
finally
{
con.Close();
}
}
It's pretty explained: It will try to open the connection and set a success message.
If it fails, it will say that the connection failed and display the error message from the catched exception.
Then, on the finally block, it will close the connection, after both try and catch has finished executing.
I'm not sure but since you're using using, you wouldn't need the finally block, for the using function will close the connection automatically.
More info on the MSDN page for the try catch finally blocks.
I am trying to connect to an SQL db to extract data and I am using Dapper for the process. I assume I am having some problems with the connection string; I am trying to access a remote sql server 2012 db but my connection keeps failing:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
namespace Data_Connection
{
public static class Program
{
static void Main(string[] args)
{
string connetionString = null;
SqlConnection cnn ;
connetionString = "Server=datanet.local.net/datadb; Database=ddata;Trusted_Connection=True";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
Console.WriteLine("Connection Open !");
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Can not open connection !");
}
}
}
}
I have never tried connecting to an sql db using c# and I am fairly new to the environment. Any help would be appreciated
Thanks!
I cannot connect to MySQL it fails on line connection.Open(), is there something wrong with my code ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySQLConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost:3316;" +
"DATABASE=mydb;" +
"UID=user;" +
"PASSWORD=password;";
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
// ...
connection.Close();
}
}
}
this is the string format I use to connect via the MySql.Data.dll version 6.1.2.0
server={0};user id={1};password={2};database={3};port={4}
so your connection string should be
server=localhost;user id=user;password=password;database=mydb;port=3316
You need to specify the Port as a separate argument in the connection string and it looks like the password key is "Pwd" instead of "Password".
See connectionstrings.com for help on the exact syntax.