突然对.NET连接MySQL数据库有点兴趣,于是乎网上到处找资料,学习MySQL的安装,MySQL的使用等等等等,终于搞定了!
最终效果就是显示数据库中数据表的数据:
首先,当然要有MySQL数据库啦,MySQL下载地址:www.mysql.com/downloads/ ,我下载的是最新版本的MySQL。
安装方法可以参考这篇文章:5.6版本MySQL的下载、安装及配置过程
如果安装完成后发现没有以下文件,说明还没有.NET平台对应的驱动,也要到官网去下载安装,方法具体可以参照这篇博文:.net连接MySQL的方法 。
其实就是对.NET平台MySql相关类库的引用
然后就是代码的编辑啦:
1.要先在配置文件中配置数据库的连接字段,和SqlServer 一样的
Web.config
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="connStr" connectionString="server =localhost;port=3306; user id = root; password = 111111; database = yc_test"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> </configuration>
2.个人根据别人的sql的助手类,写了个简单的MySql助手类o(∩_∩)o ,参照博文:自己封装的SQLHelper
MySQlHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MySql.Data.MySqlClient; using System.Configuration; using System.Data; /** *创建人:Yc *说明:数据库助手类 */ namespace MySql_Try { public class MySQlHelper { private MySqlConnection conn = null; private MySqlCommand cmd = null; private MySqlDataReader sdr; private MySqlDataAdapter sda = null; public MySQlHelper() { string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; //获取MySql数据库连接字符串 conn = new MySqlConnection(connStr); //数据库连接 } /// <summary> /// 打开数据库链接 /// </summary> /// <returns></returns> private MySqlConnection GetConn() { if(conn.State== ConnectionState.Closed) { conn.Open(); } return conn; } /// <summary> /// 关闭数据库链接 /// </summary> private void GetConnClose() { if (conn.State == ConnectionState.Open) { conn.Close(); } } /// <summary> /// 执行不带参数的增删改SQL语句或存储过程 /// </summary> /// <param name="cmdText">增删改SQL语句或存储过程的字符串</param> /// <param name="ct">命令类型</param> /// <returns>受影响的函数</returns> public int ExecuteNonQuery(string cmdText,CommandType ct) { int res; using(cmd = new MySqlCommand(cmdText,GetConn())) { cmd.CommandType = ct; res = cmd.ExecuteNonQuery(); } return res; } /// <summary> /// 执行带参数的增删改SQL语句或存储过程 /// </summary> /// <param name="cmdText">增删改SQL语句或存储过程的字符串</param> /// <param name="paras">往存储过程或SQL中赋的参数集合</param> /// <param name="ct">命令类型</param> /// <returns>受影响的函数</returns> public int ExecuteNonQuery(string cmdText,MySqlParameter[] paras,CommandType ct) { int res; using(cmd = new MySqlCommand(cmdText,GetConn())) { cmd.CommandType = ct; cmd.Parameters.AddRange(paras); res = cmd.ExecuteNonQuery(); } return res; } /// <summary> /// 执行不带参数的查询SQL语句或存储过程 /// </summary> /// <param name="cmdText">查询SQL语句或存储过程的字符串</param> /// <param name="ct">命令类型</param> /// <returns>查询到的DataTable对象</returns> public DataTable ExecuteQuery(string cmdText,CommandType ct) { DataTable dt = new DataTable(); cmd = new MySqlCommand(cmdText,GetConn()); cmd.CommandType = ct; using(sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } return dt; } /// <summary> /// 执行带参数的查询SQL语句或存储过程 /// </summary> /// <param name="cmdText">查询SQL语句或存储过程的字符串</param> /// <param name="paras">参数集合</param> /// <param name="ct">命令类型</param> /// <returns></returns> public DataTable ExecuteQuery(string cmdText,MySqlParameter[] paras,CommandType ct) { DataTable dt = new DataTable(); cmd = new MySqlCommand(cmdText,GetConn()); cmd.CommandType = ct; cmd.Parameters.AddRange(paras); using(sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } return dt; } /// <summary> /// 执行指定数据库连接字符串的命令,返回DataSet. /// </summary> /// <param name="strSql">一个有效的数据库连接字符串</param> /// <returns>返回一个包含结果集的DataSet</returns> public DataSet ExecuteDataset(string strSql) { DataSet ds = new DataSet(); sda = new MySqlDataAdapter(strSql,GetConn()); try { sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { GetConnClose(); } return ds; } } }
3. 最后就是Web页面的前台和后台了
MySql_ToConnect.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MySql_ToConnect.aspx.cs" Inherits="MySql_Try.MySql_ToConnect" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>MySql数据库连接</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html>
MySql_ToConnect.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MySql.Data.MySqlClient; using System.Configuration; using System.Data; namespace MySql_Try { public partial class MySql_ToConnect : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MySQlHelper h = new MySQlHelper(); string sql = "select * from users"; DataTable ds = h.ExecuteQuery(sql,CommandType.Text); //DataSet ds = h.ExecuteDataset(sql); GridView1.DataSource = ds; GridView1.DataBind(); } } }