衔接MYSQL数据库的办法及示例[MySQL防范]
本文“衔接MYSQL数据库的办法及示例[MySQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
衔接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.cs
using 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();
}
}
本文地址: | 与您的QQ/BBS好友分享! |