下面提供两段代码供参考,一个代码页,一个是HTML页:default2.aspx.cs下ADO.NET连接MySQL代码:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingMySql.Data.MySqlClient;
publicpartialclassDefault2:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
stringquery="select*fromguestbook";
MySqlConnectionmyConnection=newMySqlConnection("server=localhost;userid=root;password=;database=guestbook");
MySqlCommandmyCommand=newMySqlCommand(query,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
MySqlDataReadermyDataReader=myCommand.ExecuteReader();
stringbookres="";
while(myDataReader.Read()==true)
{
bookres+=myDataReader["id"];
bookres+=myDataReader["user"];
bookres+=myDataReader["pass"];
}
myDataReader.Close();
myConnection.Close();
lb1.Text=bookres;
}
}
default2.aspx下的代码
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml">
<headrunatheadrunat="server">
<title>无标题页</title>
</head>
<body>
<asp:labelIDasp:labelID=lb1runat=server/>
<formidformid="form1"runat="server">
<div>
</div>
</form>
</body>
</html>
以上结果在visualwebDeveloper2005(速成版)下测试通过!相信在visualstudio2005中也可以通过!connector/NET5.0下载地址:http://dev.mysql.com/downloads/connector/net/5.0.html在做项目前需要把Mysqlconnector/net5.0装上,然后到安装目录中找到MySql.Data.dll,将此文件拷贝到项目的bin目录下然后将usingMySql.Data.MySqlClient;这句红色的语句写到using那几行的下面。就可以了!快速上手的正确的做法是将以前写的SQLserver代码改写成mysql的,而改写的部分也很少,因为二者格式几乎一样。
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。