一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

c#连接MySQL的教程及实例方法

时间:2022-06-25 08:07:29 编辑:袖梨 来源:一聚教程网

先,我们到官方下载mysql-connector-net-5.5
Mysql的connector/net5.0下载地址:
http://dev.m*ys*q*l.com/get/Downloads/Connector-Net/mysql-connector-net-5.0.6.zip/from/pick
安装好以后,
点属性,然后点查找目标,点向上一层目录,找到Binaries.NET 2.0,然后将这个文件复制到你的工程目录下,一般这样的DLL文件会保存到bin目录下.

在 代码页里输入using Mysql.Data.MysqlClient;然后再在Page_Load函数里写MysqlConnection,在单词写到一半时提示就出来了,下 面的就不用写了吧?都已经出现”代码智能完成了”,随便写一段代码试试就可以了,和Sqlserver完全相似.
下面提供两段代码供参考,一个代码页

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string query = "select * from guestbook";
MySqlConnection myConnection = new MySqlConnection("server=localhost;user id=root;password=;database=guestbook");
MySqlCommand myCommand=new MySqlCommand(query,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
MySqlDataReader myDataReader = myCommand.ExecuteReader();
string bookres="";
while (myDataReader.Read()==true)
{
bookres+=myDataReader["id"];
bookres+=myDataReader["user"];
bookres += myDataReader["pass"];
}
myDataReader.Close();
myConnection.Close();
lb1.Text = bookres;
}
}


1、下载MySQL数据库,地址:http://www.*my**sql.com/,大小只有几十个兆而已,安装很方便,接提示即可。

2、 MySQL安装后默认是没有客户端工具的(像SQLServer的企业管理器,查询分析器等),只是一个服务器存储数据,为了方便你要再下载一个客户端工 具,有很多,推荐使用SQL Manager for MySQL,简洁小巧,功能也强大。下载:http://www.sq**lm*anager.net/en/products/mysql/manager /download

3、ASP.NET连接MySQL需要一个组件(.net本身不提供访问MySQL的驱 动)MySQL.Data.Dll,此为官方提供(纯C#开发,开源噢),有多个版本选择,采用的数据访问模式为ADO.NET,跟asp.net访问 sqlserver很像,非常简单。下载(里边有个demo):http://dev.m*y*s*ql.com/downloads/connector /net/5.1.html



c#连接MySql数据库的两种方法

测试环境:Windows XP + MySql 5.0.24 + Visual C# 2008 Exdivss Edition

By lucas 2008.12.29

一、用MySQLDriverCS连接MySQL数据库

先下载和安装MySQLDriverCS,地址:

http://sou*r*cef*orge.net/projects/mysqldrivercs/

在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中

注:我下载的是版本是 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();

}

}

}


二、通过ODBC访问mysql数据库:

参考:http://www.*m*icro*soft.com/china/community/Column/63.mspx

1. 安装Microsoft ODBC.net:我安装的是mysql-connector-odbc-3.51.22-win32.msi

2. 安装MDAC 2.7或者更高版本:我安装的是mdac_typ.exe 2.7简体中文版

3. 安装MySQL的ODBC驱动程序:我安装的是 odbc_net.msi

4. 管理工具 -> 数据源ODBC ?>配置DSN…

5. 解决方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300)

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();

}

}

}

热门栏目