0

我正在尝试编写一个更新命令,该命令使用SqlDataSource. 我让它与 1 个表一起工作,但是当我把我的INNER JOIN语法放进去时,我得到了一个错误。它说“我的语法错误。检查 MySql 手册以获取正确的语法

这是我的 ASPX 页面中的代码:

    <asp:SqlDataSource ID="AdminSalesmanDetailDS" runat="server" 
        ConnectionString="<%$ ConnectionStrings:intelliairConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:intelliairConnectionString.ProviderName %>" 
        SelectCommand="individual_AddressByIndividualID" 
        SelectCommandType="StoredProcedure" 
        UpdateCommand="UPDATE individual SET
          FarmName = @FarmName,
          FirstName = @FirstName, 
          MiddleName = @MiddleName,
          Address1 = @Address1,
          City = @City
          INNER JOIN address a ON i.IndividualID = a.IndividualID,
         WHERE IndividualID=@IndividualID">
        <SelectParameters>
            <asp:ControlParameter ControlID="gvSalesman" Name="oIndividualID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
          <UpdateParameters>
   
          <asp:ControlParameter Name="FarmName" ControlId="fvAdminSalesmanDetail$CompanyTextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="FirstName" ControlId="fvAdminSalesmanDetail$FirstNameTextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="MiddleName" ControlId="fvAdminSalesmanDetail$MiddleNameTextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="Address1" ControlId="fvAdminSalesmanDetail$Address1TextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="City" ControlId="fvAdminSalesmanDetail$cityTextBox" PropertyName="Text"/>
      </UpdateParameters>
     

    </asp:SqlDataSource>

更新

我现在可以使用 2 张桌子。但是,它会更新表中的每个个体。例如:我正在尝试更新 1 个人并将名字更改为 Mark。当我单击 update 时,它​​会将 Database First Name 中的每个人都更改为 Mark。

这是我的 ASPX 页面中的新代码:

    <asp:SqlDataSource ID="AdminSalesmanDetailDS" runat="server" 
        ConnectionString="<%$ ConnectionStrings:intelliairConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:intelliairConnectionString.ProviderName %>" 
        SelectCommand="individual_AddressByIndividualID" 
        SelectCommandType="StoredProcedure" 
     UpdateCommand="UPDATE individual i
         inner join address a
         on a.individualID =  i.individualID 
        set 
        
        i.FarmName = @FarmName,
        i.FirstName = @FirstName,
        i.LastName = @LastName,
        i.MiddleName = @MiddleName,
        i.Phone = @Phone, 
        i.PhoneExtention = @PhoneExtention,
        i.MobilPhone = @MobilPhone,
        i.Fax = @Fax, 
        i.Email = @Email,
        
        a.Address1 = @Address1,
        a.Address2 = @Address2,
        a.City = @City,
        a.State = @State,
        a.Zip = @Zip,
        a.Country = @Country
        where 
        i.IndividualID = i.IndividualID">
        <SelectParameters>
            <asp:ControlParameter ControlID="gvSalesman" Name="oIndividualID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
          <UpdateParameters>
   
          <asp:ControlParameter Name="FarmName" ControlId="fvAdminSalesmanDetail$CompanyTextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="FirstName" ControlId="fvAdminSalesmanDetail$FirstNameTextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="MiddleName" ControlId="fvAdminSalesmanDetail$MiddleNameTextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="Address1" ControlId="fvAdminSalesmanDetail$Address1TextBox" PropertyName="Text"/>
          <asp:ControlParameter Name="City" ControlId="fvAdminSalesmanDetail$cityTextBox" PropertyName="Text"/>
      </UpdateParameters>
     

    </asp:SqlDataSource>
4

1 回答 1

3

你有你JOINSET,并且你没有具体说明每个字段引用哪个表。我认为,要JOIN在 an中使用UPDATE,您需要更像这样的语法:

UpdateCommand=" UPDATE 
                    individual i
                INNER JOIN 
                    address a 
                        ON i.IndividualID = a.IndividualID 
                SET 
                    i.FarmName = @FarmName, 
                    i.FirstName = @FirstName,  
                    i.MiddleName = @MiddleName, 
                    a.Address1 = @Address1, 
                    a.City = @City 
                WHERE 
                    i.IndividualID=@IndividualID" >

编辑:根据对您问题的更新,看起来您有(在您的WHERE条款中)

i.IndividualID=i.IndividualID

这就是导致您的所有记录都被更新的原因(因为该陈述始终是正确的)。就像我上面的例子一样,你需要有

i.IndividualID=@IndividualID

这样,只有 ID 与您的参数匹配的行才会更新(大概只有一个)。

于 2012-02-16T21:01:45.290 回答