C # MySQL database connection method bitsCN.com
C # How to connect to the MySQL database
1. use MySQLDriverCS to connect to the MySQL database. download and install MySQLDriverCS first. address:
Http://sourceforge.net/projects/mysqldrivercs
Find MySQLDriver. dll in the installation folder, and add the MySQLDriver. dll to the project.
Note: The version I downloaded is a MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Data. Odbc;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using MySQLDriverCS;
Namespace mysql
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
MySQLConnection conn = null;
Conn = new MySQLConnection (new MySQLConnectionString ("localhost", "inv", "root", "831025"). AsString );
Conn. Open ();
MySQLCommand commn = new MySQLCommand ("set names gb2312", conn );
Commn. ExecuteNonQuery ();
String SQL = "select * from exchange ";
MySQLDataAdapter mda = new MySQLDataAdapter (SQL, conn );
DataSet ds = new DataSet ();
Mda. Fill (ds, "table1 ");
This. dataGrid1.DataSource = ds. Tables ["table1"];
Conn. Close ();
}
}
}
2. access the mysql database through ODBC:
Reference: http://www.microsoft.com/china/community/Column/63.mspx
1. install Microsoft ODBC.net: I installed a mysql-connector-odbc-3.51.22-win32.msi
2. install MDAC 2.7 or later: the mdac_typ.exe 2.7 simplified Chinese version is installed.
3. install MySQL ODBC Driver: I installed odbc_net.msi
4. Management Tools-> data source ODBC-> configure DSN...
5. add reference to Microsoft. Data. Odbc. dll (1.0.3300) in solution management)
6. Add using Microsoft. Data. Odbc to the code;
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Drawing;
Using System. Linq; // vs2005 does not seem to have this namespace. in c #2008, the automatically generated
Using System. Text;
Using System. Windows. Forms;
Using Microsoft. Data. Odbc;
Namespace mysql
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
String MyConString = "DRIVER = {MySQL ODBC 3.51 Driver};" +
"SERVER = localhost;" +
"DATABASE = inv;" +
"UID = root;" +
"PASSWORD = 831025;" +
"OPTION = 3 ";
OdbcConnection MyConnection = new OdbcConnection (MyConString );
MyConnection. Open ();
Console. WriteLine ("/n success, connected successfully! /N ");
String query = "insert into test values ('hello', 'Lucas ', 'Liu ')";
OdbcCommand cmd = new OdbcCommand (query, MyConnection );
// Handling exception: an error occurred while inserting duplicate records.
Try {
Cmd. ExecuteNonQuery ();
}
Catch (Exception ex ){
Console. WriteLine ("record duplicate .");
} Finally {
Cmd. Dispose ();
}
// *********************** Use the read method to read data to the textbox ******** **************
String tmp1 = null;
String tmp2 = null;
String tmp3 = null;
Query = "select * from test ";
OdbcCommand cmd2 = new OdbcCommand (query, MyConnection );
OdbcDataReader reader = pai2.executereader ();
While (reader. Read ())
{
Tmp1 = reader [0]. ToString ();
Tmp2 = reader [1]. ToString ();
Tmp3 = reader [2]. ToString ();
}
This. textBox1.Text = tmp1 + "" + tmp2 + "" + tmp3;
*/
// ************************* Use the datagridview control to display the data table ********* *****************
String MyConString = "DRIVER = {MySQL ODBC 3.51 Driver};" +
"SERVER = localhost;" +
"DATABASE = inv;" +
"UID = root;" +
"PASSWORD = 831025;" +
"OPTION = 3 ";
OdbcConnection MyConnection = new OdbcConnection (MyConString );
OdbcDataAdapter oda = new OdbcDataAdapter ("select * from customer", MyConnection );
DataSet ds = new DataSet ();
Oda. Fill (ds, "employee ");
This. dataGridView1.DataSource = ds. Tables ["employee"];
*/
MyConnection. Close ();
}
}
}
BitsCN.com