获取错误“查询输入必须包含至少一个表或查询”。

时间:2011-08-28 22:36:13

标签: asp.net vb.net ms-access ado.net

我正在使用visual studio 2010创建一个表单,您可以在其中编辑,删除或插入访问数据库中的数据。当我尝试在Web浏览器中运行该表单时,我收到错误“查询输入必须包含至少一个表或查询”。我相信问题在于我的代码/语法,但我无法确定问题的原因。我在下面添加了一些代码,任何帮助/提示将不胜感激。谢谢。

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/database.accdb" SelectCommand="SELECT * FROM [database]"
            DeleteCommand="DELETE FROM [database] WHERE ([EmployeeID] = @EmployeeID)"
            UpdateCommand="UPDATE [database] SET [Phone]=@Phone,[FirstName] = @FirstName, [LastName]= @LastName  WHERE ([EmployeeID] = @EmployeeID)"
            InsertCommand="INSERT INTO [database] (Phone,FirstName,LastName) VALUES (@Phone,@FirstName,@LastName) WHERE ([EmployeeID] = @EmployeeID)">
            <InsertParameters>
             <asp:formparameter name="Phone" formfield="tbPhone" />
             <asp:formparameter name="FirstName"  formfield="tbFirstName" />
             <asp:formparameter name="LastName"  formfield="tbLastName" />
            </InsertParameters>
        </asp:AccessDataSource>
        <asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="False" 
            DataSourceID="AccessDataSource1" DataKeyNames="EmployeeID"
            AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True"
            EmptyDataText="There are no data records to display." Width="372px">
            <Columns>
                <asp:BoundField DataField="Phone" HeaderText="Phone"
                     SortExpression="Phone" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" 
                    SortExpression="LastName" />

            </Columns>
        </asp:GridView>        
    </td>
     </tr>
    <tr>
    <td>
        Phone Number: 
        <asp:TextBox ID="tbPhone" runat="server"></asp:TextBox><br /> 
        First Name:
        <asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox> <br /> 
        Last Name:&nbsp;
        <asp:TextBox ID="tbLastName" runat="server"></asp:TextBox> <br />           
        <asp:button id="Button1" runat="server" text="New Employee" onclick="InsertEmployee" />
    </td>
    </tr>

1 个答案:

答案 0 :(得分:1)

什么是[数据库]?那是你的桌名吗?您需要指定一个表名,例如PERSONS或Employees?

SELECT * FROM [database]

尝试类似

的内容
SELECT * FROM Employee

立即尝试

SELECT Phone,FirstName,LastName,EmployeeID FROM [employees]

如果可行,则意味着Employees表中的某些字段无法处理...由于上述触发错误,这意味着代码无法处理Autonum字段。请尝试以下方法:

SELECT Phone,FirstName,LastName,CAST(EmployeeID as INT) as EmployeeID 
       FROM [employees]

尝试将where子句完全从INSERT SQL

中删除
  InsertCommand="INSERT INTO [database] (Phone,FirstName,LastName) VALUES (@Phone,@FirstName,@LastName) "