日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET 连接MySql数据库

發布時間:2023/12/9 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET 连接MySql数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ASP.NET Mysql操作類

以下連接MySql數據庫以VS2010為例,對于其他的編輯器也差不多

1. 我們需要在Mysql官網下載一個組件http://dev.mysql.com/downloads/connector/net/? 下載最新版的即可,并且安裝connector,其實僅僅只是為了得到Mysql.Data.dll ( 0.0 )

2. 在你的VS2010 解決方案管理器里面添加 Mysql.Data.dll 引用(通常在c:\program files\MySQL\MySQL Connector Net(your version here)\Assemblies\v4.0 or v2.0)

3. 在你的web.config里面添加connection:

[csharp]?view plain?copy
  • <connectionStrings>??
  • <add?name="MySQLConnString"?connectionString="Server=your_MySql_Server_IP;Port=3306;Database=databaseName;Uid=username;Pwd=yourpassword;pooling=false;"?providerName="MySql.Data.MySqlClient"/>??
  • </connectionStrings>??

  • 4. 在你的代碼前面添加using MySQL.Data.MySqlClient;

    下面是個簡單的連接Mysql 并且使用的例子

    [csharp]?view plain?copy
  • protected?void?Page_Load(object?sender,?EventArgs?e)??
  • {??
  • DataSet?ds_temp?=?GetObjects();??
  • ListBox1.DataSource?=?ds_temp;??
  • ListBox1.DataTextField?=?"yourColumn";??
  • ListBox1.DataValueField?=?"yourColumn";??
  • ListBox1.DataBind();??
  • }??
  • ??
  • protected?static?DataSet?GetObjects()??
  • {??
  • DataSet?ds_temp?=?Connection_cls.GetDataSetQuery("SELECT?yourColumn?FROM?yourtable",?null);??
  • return?ds_temp;??
  • ??
  • }??
  • ??
  • private?static?string?getHostString()??
  • {??
  • string?host?=?ConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;??
  • return?host;??
  • }??
  • public?static?DataSet?GetDataSetQuery(string?sql,?ArrayList?paramList)??
  • {??
  • using?(MySqlConnection?conn?=?new?MySqlConnection(getHostString()))??
  • {??
  • try??
  • {??
  • conn.Open();??
  • MySqlDataAdapter?da?=?new?MySqlDataAdapter(sql,?conn);??
  • da.SelectCommand.Parameters.Clear();??
  • if?(paramList?!=?null)??
  • {??
  • for?(int?i?=?0;?i?<?paramList.Count;?i++)??
  • {??
  • da.SelectCommand.Parameters.Add(paramList[i]?as?MySqlParameter);??
  • }??
  • }??
  • DataSet?ds?=?new?DataSet();??
  • da.Fill(ds);??
  • return?ds;??
  • }??
  • finally??
  • {??
  • conn.Close();??
  • }??
  • }??
  • }??



  • Mysql官方.net連接文檔:

    I'm using Visual Web Developer 2010 Express Edition (and Visual C# 2010 Express) with MySQL. This is what I did:

    1 - Download and install MySQL connector(just to get MySql.Data.dll).

    2 - VWD 2010 in the Solution Explorer add reference to MySql.Data.dll(usually in c:\program files\MySQL\MySQL Connector Net(your version here)\Assemblies\v4.0 or v2.0)

    3 - In the web.config add your string connection:
    <connectionStrings>
    <add name="MySQLConnString" connectionString="Server=your_MySql_Server_IP;Port=3306;Database=databaseName;Uid=username;Pwd=yourpassword;pooling=false;" providerName="MySql.Data.MySqlClient"/>
    </connectionStrings>

    4 - In the code behind type in: "using MySQL.Data.MySqlClient;"

    Now you can use it as in this example:

    protected void Page_Load(object sender, EventArgs e)
    {
    DataSet ds_temp = GetObjects();
    ListBox1.DataSource = ds_temp;
    ListBox1.DataTextField = "yourColumn";
    ListBox1.DataValueField = "yourColumn";
    ListBox1.DataBind();
    }

    protected static DataSet GetObjects()
    {
    DataSet ds_temp = Connection_cls.GetDataSetQuery("SELECT yourColumn FROM yourtable", null);
    return ds_temp;

    }

    private static string getHostString()
    {
    string host = ConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;
    return host;
    }
    public static DataSet GetDataSetQuery(string sql, ArrayList paramList)
    {
    using (MySqlConnection conn = new MySqlConnection(getHostString()))
    {
    try
    {
    conn.Open();
    MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
    da.SelectCommand.Parameters.Clear();
    if (paramList != null)
    {
    for (int i = 0; i < paramList.Count; i++)
    {
    da.SelectCommand.Parameters.Add(paramList[i] as MySqlParameter);
    }
    }
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds;
    }
    finally
    {
    conn.Close();
    }
    }

    ?


    }

    ?

    ?

    轉載于:https://www.cnblogs.com/qqhfeng/p/5223054.html

    總結

    以上是生活随笔為你收集整理的ASP.NET 连接MySql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。