| 订阅 | 在线投稿
分享
 
 
 

连接MYSQL数据库的方法及示例

来源:互联网  宽屏版  评论
2008-06-01 01:43:02

本文为【连接MYSQL数据库的方法及示例】的汉字拼音对照版显示拼音

lianjiemysqlshujukudefangfajishili

fangfayi

使shiyongMYSQLtuichudeMySQL Connector/Net is an ADO.NET driver for MySQL

gaizujianweiMYSQLweiADO.NET访fangwenMYSQLshujukushejide.NET访fangwenzujian

anzhuangwanchenggaizujianhouyinyongmingmingkongjianMySql.Data.MySqlClient;

使shiyongminglingxingbianyishicsc /r:MySql.Data.dll test.cs

fangfaer

tongguoODBC访fangwenMYSQLshujuku

访fangwenqianyaoxianxiazailianggezujianodbc.netheMYSQLdeODBCqudong(MySQL Connector/ODBC (MyODBC) driver)muqianwei3.51ban

anzhuangwanchenghoujiketongguoODBC访fangwenMYSQLshujuku

fangfasan

使shiyongCoreLabtuichudeMYSQL访fangwenzujianmianxiang.NET

anzhuangwanchenghouyinyongmingmingkongjianCoreLab.MySql;

使shiyongminglingbianyishicsc /r:CoreLab.MySql.dll test.csyixiawei访fangwenMYSQLshujukushilibianyizhilingcsc /r:CoreLab.MySql.dll /r:MySql.Data.dll test.csusing System;

using System.Net;

using System.Text;

using CoreLab.MySql;

using System.Data.Odbc;

using MySql.Data.MySqlClient;class ConnectMySql

{

public void Connect_CoreLab()

{

string constr = "User Id=root;Host=localhost;Database=qing;passWord=qing";

MySqlConnection mycn = new MySqlConnection(constr);

mycn.Open();

MySqlCommand mycm = new MySqlCommand("select * from shop",mycn);

MySqlDataReader msdr = mycm.ExecuteReader();

while(msdr.Read())

{

if (msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0));

}

}

msdr.Close();

mycn.Close();

}

public void Connect_Odbc()

{

//string MyConString ="DSN=MySQL;UID=root;PWD=qing";

string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +

"SERVER=localhost;" +

"DATABASE=test;" +

"UID=root;" +

"PASSWORD=qing;" +

"OPTION=3";

OdbcConnection MyConn = new OdbcConnection(MyConString);

MyConn.Open();

OdbcCommand mycm = new OdbcCommand("select * from hello",MyConn);

OdbcDataReader msdr = mycm.ExecuteReader();

while(msdr.Read())

{

if (msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0));

}

}

msdr.Close();

MyConn.Close();

}

public void Connect_Net()

{

string myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=qing";

MySqlConnection mycn = new MySqlConnection(myConnectionString);

mycn.Open();

MySqlCommand mycm = new MySqlCommand("select * from hello",mycn);

MySqlDataReader msdr = mycm.ExecuteReader();

while(msdr.Read())

{

if (msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0));

}

}

msdr.Close();

mycn.Close();

}

public static void Main()

{

ConnectMySql ms = new ConnectMySql();

ms.Connect_CoreLab();

ms.Connect_Odbc();

Connect_Net();

}

}

原文
 
连接mysql数据库的方法及示例 方法一: 使用MYSQL推出的MySQL Connector/Net is an ADO.NET driver for MySQL 该组件为MYSQL为ADO.NET访问MYSQL数据库设计的.NET访问组件。 安装完成该组件后,引用命名空间MySql.Data.MySqlClient; 使用命令行编译时:csc /r:MySql.Data.dll test.cs 方法二: 通过ODBC访问MYSQL数据库 访问前要先下载两个组件:odbc.net和MYSQL的ODBC驱动(MySQL Connector/ODBC (MyODBC) driver)目前为3.51版 安装完成后,即可通过ODBC访问MYSQL数据库 方法三: 使用CoreLab推出的MYSQL访问组件,面向.NET 安装完成后,引用命名空间:CoreLab.MySql; 使用命令编译时:csc /r:CoreLab.MySql.dll test.cs以下为访问MYSQL数据库实例编译指令:csc /r:CoreLab.MySql.dll /r:MySql.Data.dll test.csusing System; using System.Net; using System.Text; using CoreLab.MySql; using System.Data.Odbc; using MySql.Data.MySqlClient;class ConnectMySql { public void Connect_CoreLab() { string constr = "User Id=root;Host=localhost;Database=qing;passWord=qing"; MySqlConnection mycn = new MySqlConnection(constr); mycn.Open(); MySqlCommand mycm = new MySqlCommand("select * from shop",mycn); MySqlDataReader msdr = mycm.ExecuteReader(); while(msdr.Read()) { if (msdr.HasRows) { Console.WriteLine(msdr.GetString(0)); } } msdr.Close(); mycn.Close(); } public void Connect_Odbc() { //string MyConString ="DSN=MySQL;UID=root;PWD=qing"; string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=qing;" + "OPTION=3"; OdbcConnection MyConn = new OdbcConnection(MyConString); MyConn.Open(); OdbcCommand mycm = new OdbcCommand("select * from hello",MyConn); OdbcDataReader msdr = mycm.ExecuteReader(); while(msdr.Read()) { if (msdr.HasRows) { Console.WriteLine(msdr.GetString(0)); } } msdr.Close(); MyConn.Close(); } public void Connect_Net() { string myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=qing"; MySqlConnection mycn = new MySqlConnection(myConnectionString); mycn.Open(); MySqlCommand mycm = new MySqlCommand("select * from hello",mycn); MySqlDataReader msdr = mycm.ExecuteReader(); while(msdr.Read()) { if (msdr.HasRows) { Console.WriteLine(msdr.GetString(0)); } } msdr.Close(); mycn.Close(); } public static void Main() { ConnectMySql ms = new ConnectMySql(); ms.Connect_CoreLab(); ms.Connect_Odbc(); Connect_Net(); } }
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有