当前位置:数据库 > MySQL >>

asp.net 连接mysql数据库方法

下面提供一个关于mysql教程数据库教程连接代码,我们现在利用asp教程.net C#的方法来连接mysql数据库并显示数据,代码实现如下。

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server">

    void Page_Load(object sender, EventArgs e) {
   
        // TODO: Update the ConnectionString and CommandText values for your application
        string ConnectionString = @"driver={MySQL ODBC 3.51 Driver};server=localhost;database=books;uid=band;pwd=letmein;";
        string CommandText = "select * from Publisher";
   
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
   
        myConnection.Open();
   
        DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        DataGrid1.DataBind();
   
        // close the connection
        myConnection.Close();
    }

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <h2>Simple Data Report
    </h2>
    <hr size="1" />
    <form runat="server">
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" EnableViewState="False">
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

补充:数据库,mysql教程
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,