萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> asp.net 連接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>

copyright © 萬盛學電腦網 all rights reserved