突然對.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(); } }}