How to connect to a MySQL database using the mysql .NET connector in C#.
Download and install the MySql.Net connector.
Add a reference to it in your project, that is - click on "Project'->'Add reference', on your '.net assembly browser' tab and choose the library, make sure you're adding the 'Assembly' (the type is shown in the window just after you select the file).
And here is sample code from a working program:
using MySql.Data.MySqlClient;
namespace my_namespace_whatever
{
public partial class MainForm : Form
{
private MySqlConnection MyConn;
private MySqlCommand MyCom;
private MySqlDataAdapter MyDR;
private string ConnStr;
public MainForm()
{
InitializeComponent();
// now the interesting part
ConnStr = "SERVER=localhost;DATABASE=my_database;UID=my_user;PWD=my_password;";
MyConn = new MySqlConnection(ConnStr);
if (MyConn.State == ConnectionState.Closed)
{
MyConn.Open();
}
if (MyConn.State != ConnectionState.Open)
{
MessageBox.Show("Could not connect");
}
// A.S.O
}
}
Se afișează postările cu eticheta Mysql. Afișați toate postările
Se afișează postările cu eticheta Mysql. Afișați toate postările
miercuri, 30 noiembrie 2011
MySql .NET connector example
system.data.odbc.odbcexception: error [im002] [microsoft][odbc driver manager] data source name not found and no default driver specified
Trying to use MySQL ODBC driver with a C# program to make a connection to the MySQL database.
Downloaded the latest MySQL ODBC 5.1 Driver from mysql's website, yes, the right version (32-bit for my Windows, check your version - you might need the 64-bit one)
Installed driver.
Created DSN in User DSN (control panel->administrative tools->data sources, and NO, it is NOT needed to create the ODBC Data Source in your System DSN or other, just in User DSN).
Yes, entered correct server name, username, password.
Now going to my C# program, at Connection.Open() - i get the following error: system.data.odbc.odbcexception: error [im002] [microsoft][odbc driver manager] data source name not found and no default driver specified
WHAT in God's name is the problem????
I must say that i've installed and used the mysql odbc connector in the past too, and the connection went smoothly then..
Yes, the problem is the connection string. This is what i had:
Banged head, ripped clothes off, cut veins, and then success: copy and paste the EXACT ODBC driver name, and now i have this:
Please notice the string at 'driver=', it must be the same as the ODBC Driver Name, in my case: MYSQL ODBC 5.1 Driver
You find the driver name under Control Panel -> Administrative Tools -> Data Sources (ODBC) , in the 'driver' column (obvious, eh?), next to my own MySQL ODBC Data Source.
Pheewww, that was a bugger.
PS: Oh, and NO, you don't need to add any references to the mysql library either in your project. This is the ODBC method, and you are not using any mysql objects/references here. You must include though your System.Data. If you want to go the mysql .net connection method, search for another post.
Downloaded the latest MySQL ODBC 5.1 Driver from mysql's website, yes, the right version (32-bit for my Windows, check your version - you might need the 64-bit one)
Installed driver.
Created DSN in User DSN (control panel->administrative tools->data sources, and NO, it is NOT needed to create the ODBC Data Source in your System DSN or other, just in User DSN).
Yes, entered correct server name, username, password.
Now going to my C# program, at Connection.Open() - i get the following error: system.data.odbc.odbcexception: error [im002] [microsoft][odbc driver manager] data source name not found and no default driver specified
WHAT in God's name is the problem????
I must say that i've installed and used the mysql odbc connector in the past too, and the connection went smoothly then..
Yes, the problem is the connection string. This is what i had:
ConnStr = "DRIVER={MYSQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=my_database;UID=my_username;PWD=my_password;OPTION=1";
Banged head, ripped clothes off, cut veins, and then success: copy and paste the EXACT ODBC driver name, and now i have this:
ConnStr = "DRIVER={MYSQL ODBC 5.1 Driver};SERVER=localhost;DATABASE=my_database;UID=my_username;PWD=my_password;OPTION=1";
Please notice the string at 'driver=', it must be the same as the ODBC Driver Name, in my case: MYSQL ODBC 5.1 Driver
You find the driver name under Control Panel -> Administrative Tools -> Data Sources (ODBC) , in the 'driver' column (obvious, eh?), next to my own MySQL ODBC Data Source.
Pheewww, that was a bugger.
PS: Oh, and NO, you don't need to add any references to the mysql library either in your project. This is the ODBC method, and you are not using any mysql objects/references here. You must include though your System.Data. If you want to go the mysql .net connection method, search for another post.
Abonați-vă la:
Postări (Atom)