2014-01-29 29 views
0

我嘗試獲取SqlDataSource時遇到問題。這裏是ASP.net代碼:SqlDataSource中奇怪的SQL異常

<asp:SqlDataSource id="SqlDataSource2" runat="server" 
      ConnectionString="<%$ ConnectionStrings:connection %>" 
      SelectCommand="SELECT [term] [FROM[Ceremony]] order by term"> 
    </asp:SqlDataSource> 

異常詳細信息是: System.Data.SqlClient.SqlException:閉合的引號字符 字符串 'FROM [禮]通過長期訂單'

後我只是不知道什麼是錯誤的asp.net代碼?我的C#代碼正在工作,並且SQL服務器已啓動並正在運行。我進入SQL工具來測試選擇命令,它的工作原理!如果任何人都可以指出這個代碼的任何問題,那就太棒了!

p.s.連接字符串也正常工作。

回答

4

圍繞FROM子句的括號不是必需的。只有在列/表名具有空格或者它們是保留的SQL關鍵字時才需要使用它們。

更改SQL語句:

SELECT [term] FROM [Ceremony] order by term 
+0

感謝您的回覆這麼快,我會嘗試一下:-) –

2

嘗試......

<asp:SqlDataSource id="SqlDataSource2" runat="server" 
      ConnectionString="<%$ ConnectionStrings:connection %>" 
      SelectCommand="SELECT [term] FROM [Ceremony] order by term"> 
    </asp:SqlDataSource> 

你有一個奇怪的嵌套的括號語法。不要將「FROM」字放在括號中。

0
<asp:SqlDataSource id="SqlDataSource2" runat="server" 
      ConnectionString="<%$ ConnectionStrings:connection %>" 
      SelectCommand="SELECT [term] FROM[Ceremony] order by [term]"> 
    </asp:SqlDataSource> 

如果長期和儀式不是一個關鍵字,然後我覺得不需要有[]