吉吉于

free

Mac OS 下Asp.net连接Mysql

我的联想笔记本坏了,不能联网,只能用mac写C#了,还没用mono正式做过东西,这次试试吧。

SQLserver没有mac版,mysql搭配Asp.net貌似也不错。

首先,需要下载Connector/Net,地址:http://dev.mysql.com/downloads/connector/net/5.2.html

2,安装Mysql.data.dll到mono framework

su命令行下,sh-3.2# gacutil -i /z/mysql.data.dll
Installed /z/mysql.data.dll into the gac (/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/gac)

(此dll为下载的文件)

3,创建文件,插入代码:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="MySql.Data.MySqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>CD cat</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <script runat="server">
    private void Page_Load(Object sender, EventArgs e)
    {
       string connectionString = "Server=localhost;Database=mono;User ID=root;Pooling=false;";
       MySqlConnection dbcon = new MySqlConnection(connectionString);
       dbcon.Open();

       MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM artist", dbcon);
       DataSet ds = new DataSet();
       adapter.Fill(ds, "result");

       dbcon.Close();
       dbcon = null;

       ArtistsControl.DataSource = ds.Tables["result"];
       ArtistsControl.DataBind();
    }
    </script>

  </head>

  <body>
    <h1>Artists</h1>
    <asp:DataGrid runat="server" id="ArtistsControl" />
  </body>

</html>

4,在cs文件中引入mysql.data命名空间

5,测试:

转载请注明:于哲的博客 » Mac OS 下Asp.net连接Mysql