【发布时间】:2020-04-21 21:26:11
【问题描述】:
我正在制作一个 ASP.net Web 应用程序,我正在尝试用来自数据库的信息填充一个表。但是,我收到了错误:
异常详细信息:System.Data.SqlClient.SqlException:关键字“FROM”附近的语法不正确。
关键字“AS”附近的语法不正确。
这里是代码
conn.Open();
// Make for new table with covalent
SqlCommand command = new SqlCommand("SELECT [compound_name], cc.[nonmetal1_quantity] AS [nonMetal1_quantity], (SELECT" +
"ion.formula FROM NonMetal AS ion WHERE ion.[nonmetal_id] = cc.[nonmetal1_id]) AS[nonMetal1]," +
"cc.[nonmetal2_quantity] as [nonMetal2_quantity], (SELECT ion.formula FROM NonMetal AS ion WHERE ion.[nonmetal_id] = cc.[nonmetal2_id]) AS[nonMetal2]," +
"c.compound_id AS [compound_id] FROM Compound as c, Covalent AS cc, NonMetal AS n WHERE c.[compound_id] = cc.[compound_id] ORDER BY c.[compound_name] ASC", conn);
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table); //Line where the error is thrown
CompoundTable.DataSource = table;
CompoundTable.DataBind();
以及匹配的网格视图。
<asp:GridView ID="CompoundTable" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" PageSize="20" OnSorting="CompoundTable_Sorting" OnPageIndexChanging="CompoundTable_PageIndexChanging" OnRowCancelingEdit="CompoundTable_CancelEdit" OnRowEditing="CompoundTable_Edit" OnRowUpdating="CompoundTable_Update" onrowdatabound="CompoundTable_DataBound" CellPadding="4">
<Columns>
<asp:TemplateField Visible="false" HeaderText="ID">
<ItemTemplate>
<asp:Label ID="compound_id" runat="server" Text='<%#Eval("compound_id") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Compound">
<ItemTemplate>
<asp:Label ID="compound_name" runat="server" Text='<%#Eval("compound_name") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="name_txt" runat="server" Text='<%#Eval("compound_name") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Non-Metal 1">
<ItemTemplate>
<asp:Label ID="nonMetal1" runat="server" Text='<%#Eval("nonMetal1") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID ="nonMetal1_txt" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="nonMetal1_quantity" runat="server" Text='<%#Eval("nonMetal1_quantity") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="nonMetal1_quantity_txt" runat="server" Text='<%#Eval("nonMetal1_quantity") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Non-Metal 2">
<ItemTemplate>
<asp:Label ID="nonMetal2" runat="server" Text='<%#Eval("nonMetal2") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID ="nonMetal2_txt" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="nonMetal2_quantity" runat="server" Text='<%#Eval("nonMetal2_quantity") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID ="nonMetal2_quantity_txt" runat="server" Text='<%#Eval("nonMetal2_quantity") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="compound_id" DataNavigateUrlFormatString="DeleteCompound.aspx?compound={0}" Text="Delete Compound" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LBEdit" runat="server" CommandName="Edit" >Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LBCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
<asp:LinkButton ID="LBUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
谢谢。
【问题讨论】:
-
你有两个未闭合的括号用于 (SELECT
-
您应该做的第一件事是在执行查询之前设置一个断点并在 Watch 窗口中检查
command.CommandText。许多问题只需在继续之前查看值即可发现。考虑改用逐字字符串。连接容易出错。