使用mysql.data.mysqlclient实现C#连接MySQL数据库
MySQL提供的是一个dll文件——“MySql.Data.dll”,目前我用的就是这个dll文件,将这个文件(见附件)放到网站的bin目录下,就可以使用了。而且和SQL Server的用法并没有太大的出入,只是应用的名称不一样而已。
代码如下:
1.default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<form id="form1" runat="server">
<div>
<asp:Table ID="tblTest" runat="server">
<asp:TableHeaderRow ID="tblRowHeader" runat="server">
<asp:TableHeaderCell ID="tblCellHeader" runat="server" Text="新闻标题"></asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</div>
</form>
</body>
</html>
2.default.aspx.cs:
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
MySqlConnection mySqlConn = new MySqlConnection(connStr);
string sqlStr = "SELECT name FROM t_document ORDER BY id desc";
mySqlConn.Open();
MySqlCommand mySqlCmd = new MySqlCommand(sqlStr,mySqlConn);
MySqlDataReader mySqlRdr = mySqlCmd.ExecuteReader();
while (mySqlRdr.Read())
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = mySqlRdr["name"].ToString();
tr.Cells.Add(tc);
tblTest.Rows.Add(tr);
}
}
}
代码如下:
1.default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<form id="form1" runat="server">
<div>
<asp:Table ID="tblTest" runat="server">
<asp:TableHeaderRow ID="tblRowHeader" runat="server">
<asp:TableHeaderCell ID="tblCellHeader" runat="server" Text="新闻标题"></asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</div>
</form>
</body>
</html>
2.default.aspx.cs:
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
MySqlConnection mySqlConn = new MySqlConnection(connStr);
string sqlStr = "SELECT name FROM t_document ORDER BY id desc";
mySqlConn.Open();
MySqlCommand mySqlCmd = new MySqlCommand(sqlStr,mySqlConn);
MySqlDataReader mySqlRdr = mySqlCmd.ExecuteReader();
while (mySqlRdr.Read())
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = mySqlRdr["name"].ToString();
tr.Cells.Add(tc);
tblTest.Rows.Add(tr);
}
}
}