简体   繁体   English

选择语句中的asp.net c#mysql方括号

[英]asp.net c# mysql square brackets in select statement

I am having an issue with the way asp.net/c# is generating mySQL select statements when setting up my datasource control. 设置数据源控件时,asp.net / c#生成mySQL select语句的方式出现问题。

I am pretty sure I have a setting set wrong, but cannot figure out what it is. 我很确定我的设置错误,但是无法弄清楚它是什么。

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:demers_dbConnectionString3 %>" 
        ProviderName="<%$ ConnectionStrings:demers_dbConnectionString3.ProviderName %>" 
        SelectCommand="SELECT * FROM [test_table]"></asp:SqlDataSource>

As you can see, the brackets around test_table are square, and this statement throws a syntax error. 如您所见,test_table的方括号是方括号,此语句引发语法错误。

When I manually change the brackets to: 当我手动将方括号更改为:

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:demers_dbConnectionString3 %>" 
        ProviderName="<%$ ConnectionStrings:demers_dbConnectionString3.ProviderName %>" 
        SelectCommand="SELECT * FROM (test_table)"></asp:SqlDataSource>

The error goes away and my page runs. 错误消失并且我的页面运行。

I will have to create a lot of mySQL links as I develop my site, so I was hoping to fix the setting that I have wrong. 在开发网站时,我将必须创建许多mySQL链接,因此我希望修复错误的设置。

Ideas??? 想法???

This is the associated config file. 这是关联的配置文件。

    <?xml version="1.0"?>
    <configuration>
    <connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
    providerName="System.Data.SqlClient" />
    <add name="demers_dbConnectionString" connectionString="server=localhost;user id=MYID;password=MYPASSWORD;database=demers_db"
    providerName="MySql.Data.MySqlClient" />
    </connectionStrings>
    <system.web>
    <compilation debug="true" targetFramework="4.0" />

No need of those [] ; 不需要那些[] since you are using MySQL you should use backtique (`) for escaping. 由于您使用的是MySQL ,因此应使用backtique(`)进行转义。 Your select command should look like 您的选择命令应该看起来像

SelectCommand="SELECT * FROM `test_table`" 

(or) just below since there is no spaces in your table name (或)在下面,因为您的表名中没有空格

SelectCommand="SELECT * FROM test_table"

Also make sure that you are using MySQL connector provider ProviderName="MySql.Data.MySqlClient" 另外,请确保您正在使用MySQL连接器提供ProviderName="MySql.Data.MySqlClient"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM