ASP .NET - 配置SQLDataSource以使用MySQL .NET Connector

时间:2009-06-30 19:44:08

标签: asp.net mysql

我正在使用VS 2008。

在论坛/堆栈上查看并没有提供关于如何在ASP .NET站点中使用MySQL的明确答案。

如何使用MySQL Connector NET提供程序配置SQLDataSource以使用MySQL?

我不想使用ODBC驱动程序 - 我可以使用它。连接器已添加为项目的参考,并在web.config中显示为:

<add assembly="MySql.Data, Version=5.2.2.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>

我还尝试在以下位置手动创建一个部分:

<add name="MYSQL" connectionString="Server=localhost;Database=data;Uid=root;Pwd=1234;" providerName="MySql.Data" />

我拥有的MySQL Connector版本是5.2.2.0

4 个答案:

答案 0 :(得分:3)

<\ n>在web.conf文件中,添加以下行: <connectionStrings> <add name="connMysql" connectionString ="Server=localhost;Database=transcode;Uid=root;" providerName="MySql.Data.MySqlClient"/> </connectionStrings>

在.aspx文件中,您可以添加以下行:

<asp:SqlDataSource ID="Datacmd" runat="server" ConnectionString="<%$ConnectionStrings:connMysql %>" ProviderName="<%$ ConnectionStrings:connMysql.providerName%>" SelectCommand="SELECT command.id,server.ip,command.name,command.cmd,command.input_ip,command.output_ip,command.pid,command.status FROM server,command WHERE command.server_id=server.id " </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" DataSourceID="Datacmd" AutoGenerateColumns="False" ForeColor="#003300"> <Columns> ............................. </Columns> </asp:GridView>

答案 1 :(得分:2)

MySQl and ASP.NET tutorial

也许这会有所帮助?

答案 2 :(得分:1)

我能让设计时绑定工作的唯一方法是使用ODBC驱动程序。 .Net驱动程序根本不起作用。您必须安装MySQL ODBC驱动程序,重新启动visual studio,然后将连接字符串添加到web.config。

在Web.Config中:

<connectionStrings>
<add name="ConnectionStringODBC" connectionString="Driver={MySQL ODBC 5.3 ANSI Driver};server=localhost;database=myproviewer;uid=myproviewer;password=mypassword" providerName="System.Data.Odbc" />
</connectionStrings>

在ASPX中:

<dx:ASPxGridView ID="gridTagGroups" runat="server" DataMember="DefaultView" Theme="Office2010Blue" Width="95%" Font-Size="8pt" AutoGenerateColumns="False" DataSourceID="SqlDataSourceTagGroup">
    <Settings ShowHeaderFilterButton="true" ShowFilterRow="true" ShowFilterRowMenu="true" ShowGroupPanel="true" />
    <SettingsPager PageSize="20" Position="TopAndBottom" AlwaysShowPager="True">
        <PageSizeItemSettings Caption="Items Per Page:" Items="10, 15, 20, 25, 50" Visible="True" Position="Right">
        </PageSizeItemSettings>
    </SettingsPager>
    <SettingsDataSecurity AllowDelete="False" AllowEdit="False" AllowInsert="False" />
    <Columns>
        <dx:GridViewDataTextColumn FieldName="TagCatId" VisibleIndex="0">
            <EditFormSettings Visible="False" />
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="TagCatName" VisibleIndex="1">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="OrderIndex" VisibleIndex="2">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="HiddenFromSearch" VisibleIndex="3">
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="MediaType" VisibleIndex="4">
        </dx:GridViewDataTextColumn>
    </Columns>
    <SettingsBehavior AllowFocusedRow="True" AllowSelectSingleRowOnly="True" />
</dx:ASPxGridView>
<asp:SqlDataSource ID="SqlDataSourceTagGroup" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringODBC %>" ProviderName="<%$ ConnectionStrings:ConnectionStringODBC.ProviderName %>" 
    SelectCommand="SELECT * FROM tagcat ORDER BY OrderIndex" 
    DeleteCommand="DELETE FROM tagcat WHERE TagCatId = ?" 
    InsertCommand="INSERT INTO tagcat (TagCatId, TagCatName, OrderIndex, HiddenFromSearch, MediaType) VALUES (?, ?, ?, ?, ?)" 
    UpdateCommand="UPDATE tagcat SET TagCatName = ?, OrderIndex = ?, HiddenFromSearch = ?, MediaType = ? WHERE TagCatId = ?">
    <DeleteParameters>
        <asp:Parameter Name="TagCatId" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="TagCatId" Type="Int32" />
        <asp:Parameter Name="TagCatName" Type="String" />
        <asp:Parameter Name="OrderIndex" Type="Int32" />
        <asp:Parameter Name="HiddenFromSearch" Type="Object" />
        <asp:Parameter Name="MediaType" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="TagCatName" Type="String" />
        <asp:Parameter Name="OrderIndex" Type="Int32" />
        <asp:Parameter Name="HiddenFromSearch" Type="Object" />
        <asp:Parameter Name="MediaType" Type="String" />
        <asp:Parameter Name="TagCatId" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

答案 3 :(得分:0)

创建基本MySQL连接的C#版本是:

<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<script language="C#" runat="server">

private void Page_Load(Object sender ,EventArgs e)
{   
    MySqlConnection myConnection = new MySqlConnection();
    MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
    DataSet myDataSet = new DataSet();
    string strSQL;
    int iRecordCount;

    myConnection = new MySqlConnection("server=localhost; user id=root; password=ii33uuqwerty; database=wlc_data; pooling=false;");

    strSQL = "SELECT * FROM troutetracking LIMIT 100;";

    myDataAdapter = new MySqlDataAdapter(strSQL, myConnection);
    myDataSet = new DataSet();
    myDataAdapter.Fill(myDataSet, "mytable");

    MySQLDataGrid.DataSource = myDataSet;
    MySQLDataGrid.DataBind();

}

简单的MySQL数据库查询