[转]VS2005连接Mysql
本文转自:http://hi.baidu.com/mpzzjcnetbbgjyq/item/c9da90f9ebc0071ed7ff8c82
1.下载并安装mysql-connector-net
我安装的是mysql-connector-net-5.0.9.exe;
2.右键项目名->添加引用,选择MySql.Data
3.添加using MySql.Data.MySqlClient;
public static class DBHelper
{
private static MySqlConnection conn;
private static MySqlCommand com;
//创建并打开连接
public static void connOpen()
{
conn = new MySqlConnection("server=localhost;user id=root; password=root; database=Stu; pooling=false;port=3306");
conn.Open();
}
//关闭连接
public static void connClose()
{
conn.Close();
}
//执行增删改操作,返回结果true/false
public static bool GetNonQuery(string sql)
{
try
{
connOpen();
com = new MySqlCommand(sql, conn);
int i = com.ExecuteNonQuery();
if (i == 1)
{
return true;
}
}
finally
{
connClose();
}
return false;;
}
//执行查询操作,返回MySqlDataReader
public static MySqlDataReader GetReader(string sql)
{
connOpen();
com = new MySqlCommand(sql, conn);
MySqlDataReader reader= com.ExecuteReader();
return reader;
}
}
posted on 2012-11-20 10:41 freeliver54 阅读(1128) 评论(1) 编辑 收藏 举报