cxilデータベースに接続する二つの方法
4776 ワード
1、MySQLDriverCSでMySQLデータベースをダウンロードしてインストールします。アドレス:
http://sourceforge.net/projects/mysqldrivercs/
インストールフォルダの下にMySQLDriver.dllを見つけ、MySQLDriver.dllを項目に追加します。
私がダウンロードしたのはバージョンです。 MySQLDriverCS-n-EasyQueryTools-4.4.1-Dott Net 2.0.exe
参考:http://www.microsoft.com/china/community/Column/63.mspx
1. Microsoft ODBC.netをインストールします。私がインストールしているのはmysql-connector-odbc-35.22-win 32.msiです。
2. MDC 2.7をインストールするか、またはより高いバージョン:私がインストールしたのはmdac_です。typ.exe 2.7簡体字中国語版
3. MySQLのODBCドライバをインストールします。インストールしたのは odbc_net.msi
4. 管理ツール -> データソースODBC–構成DSN...
5. ソリューション管理に参照を追加 Microsoft.Data.Odbc.dll(1.0.300)
6. コードに参照を追加 using Microsoft.Data.Odbc。
http://sourceforge.net/projects/mysqldrivercs/
インストールフォルダの下にMySQLDriver.dllを見つけ、MySQLDriver.dllを項目に追加します。
私がダウンロードしたのはバージョンです。 MySQLDriverCS-n-EasyQueryTools-4.4.1-Dott Net 2.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、ODBCを通じてmysqlデータベースにアクセスする:参考:http://www.microsoft.com/china/community/Column/63.mspx
1. Microsoft ODBC.netをインストールします。私がインストールしているのはmysql-connector-odbc-35.22-win 32.msiです。
2. MDC 2.7をインストールするか、またはより高いバージョン:私がインストールしたのはmdac_です。typ.exe 2.7簡体字中国語版
3. MySQLのODBCドライバをインストールします。インストールしたのは odbc_net.msi
4. 管理ツール -> データソースODBC–構成DSN...
5. ソリューション管理に参照を追加 Microsoft.Data.Odbc.dll(1.0.300)
6. コードに参照を追加 using Microsoft.Data.Odbc。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq; //vs2005 , c#2008
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);
// :
try{
cmd.ExecuteNonQuery();
}
catch(Exception ex){
Console.WriteLine("record duplicate.");
}finally{
cmd.Dispose();
}
//*********************** read textbox**********************
string tmp1 = null;
string tmp2 = null;
string tmp3 = null;
query = "select * from test ";
OdbcCommand cmd2 = new OdbcCommand(query, MyConnection);
OdbcDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
tmp1 = reader[0].ToString();
tmp2 = reader[1].ToString();
tmp3 = reader[2].ToString();
}
this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;
*/
//************************ datagridview **************************
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();
}
}
}