在代碼頁裡輸入using Mysql.Data.MysqlClient;然後再在Page_Load函數裡寫MysqlConnection,在單詞寫到一半時提示就出來了,下面的就不用寫了吧?都已經出現”代碼智能完成了”,隨便寫一段代碼試試就可以了,和Sqlserver完全相似.
下面提供兩段代碼供參考,一個代碼頁,一個是HTML頁:
default2.aspx.cs下的代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection myConnection = new MySqlConnection("server=localhost;userid=root; password=root;database=guestword");
String query = "select * from leaveword";
MySqlCommand myCommand = new MySqlCommand(query,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
MySqlDataReader myDataReader =myCommand.ExecuteReader();
string id = "";
string title = "";
string content = "";
while(myDataReader.Read()==true){
id+=myDataReader["id"];
title+=myDataReader["title"];
content+=myDataReader["content"];
}
myDataReader.Close();
myConnection.Close();
// Label1.Text = cont.ToString();
Response.Write(id+"<BR>");
Response.Write(title+"<BR>");
Response.Write(content+"<BR>");
}
}
default2.aspx下的代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>無標題頁</title>
</head>
<body>
<asp:label ID=Label1runat=server/>
<form id="form1" runat="server">
<div>
asp.net連接Mysql - 513394217 - ㄨiao愛
</div>
</form>
</body>
</html>