简体   繁体   English

存储过程包含太多参数

[英]Stored Procedure has too many parameters


Posibale Duplicate Getting 'too many parameters passed' to stored procedure on ASPX page Posibale Duplicate将“太多参数传递给ASPX页面上的存储过程”


I'm using a SqlDataSource control for some update and delete functionality within a GridView. 我正在使用SqlDataSource控件在GridView中进行一些更新和删除功能。 I keep getting the runtime error Procedure or function spUpdateTest has too many arguments specified. 我一直得到运行时错误Procedure or function spUpdateTest has too many arguments specified. When I run the stored procedure in SQL Server it works fine. 当我在SQL Server中运行存储过程时,它工作正常。 When I click on the delete portion of the GridView, that works. 当我点击GridView的删除部分时,这是有效的。 However, when I try to update I get the above error. 但是,当我尝试更新时,我得到上述错误。

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:dbcsDrugCards %>" 
            DeleteCommand="spDeleteTest" DeleteCommandType="StoredProcedure" 
            InsertCommand="spInsertTest" InsertCommandType="StoredProcedure" 
            SelectCommand="spGetAllTest" SelectCommandType="StoredProcedure" 
            UpdateCommand="spUpdateTest" UpdateCommandType="StoredProcedure">
            <DeleteParameters>
                <asp:Parameter Name="PatientId" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="PatientId" Type="Int32" />
                <asp:Parameter Name="RaceId" Type="Int32" />
                <asp:Parameter Name="CountyId" Type="Int32" />
                <asp:Parameter Name="Gender" Type="String" />
                <asp:Parameter Name="DateOfBirth" Type="DateTime" />
                <asp:Parameter Name="SesId" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="PatientId" Type="Int32" />
                <asp:Parameter Name="RaceId" Type="Int32" />
                <asp:Parameter Name="CountyId" Type="Int32" />
                <asp:Parameter Name="Gender" Type="String" />
                <asp:Parameter Name="DateOfBirth" Type="DateTime" />
                <asp:Parameter Name="SesId" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>

You can see the six parameters in the SqlDataSource. 您可以在SqlDataSource中看到六个参数。 Now here is my stored procedure 现在这是我的存储过程

create proc spUpdateTest  
 @PatientId int  
,@RaceId int  
,@CountyId int  
,@Gender varchar(50)  
,@DateOfBirth date  
,@SesId int  
as  
begin  
update t  
set  t.raceid = @RaceId  
 ,t.countyId = @CountyId  
 ,t.gender = @Gender  
 ,t.DateOfBirth = @DateOfBirth  
 ,t.SocioEconomicStatusId = @SesId  
from test as t  
where t.patientId = @PatientId  
end  

both have six parameters. 两者都有六个参数。 GridView for good measure in case it makes a difference GridView是一个很好的衡量标准,以防万一

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" DataKeyNames="patientId" DataSourceID="SqlDataSource1" 
            ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="patientId" HeaderText="patientId" ReadOnly="True" 
                    SortExpression="patientId" />
                <asp:BoundField DataField="RaceId" HeaderText="RaceId" 
                    SortExpression="RaceId" />
                <asp:BoundField DataField="CountyId" HeaderText="CountyId" 
                    SortExpression="CountyId" />
                <asp:BoundField DataField="Gender" HeaderText="Gender" 
                    SortExpression="Gender" />
                <asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" 
                    SortExpression="DateOfBirth" />
                <asp:BoundField DataField="SocioEconomicStatusId" 
                    HeaderText="SocioEconomicStatusId" SortExpression="SocioEconomicStatusId" />
                <asp:BoundField DataField="DateEnrolled" HeaderText="DateEnrolled"  ReadOnly="true"
                    SortExpression="DateEnrolled" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

I don't know if it means anything, but I do have one field, DateEnrolled which is not meant to be edited by the user and is saved to the database as GetDate() whenever a Test object is added to the database. 我不知道它是否意味着什么,但我确实有一个字段, DateEnrolled不是由用户编辑的,只要Test对象被添加到数据库,就会以GetDate()形式保存到数据库中。

C# C#

public static void UpdateTest(int PatientId, int raceID, int countyId, string gender
            , DateTime dateOfBirth, int sesId)
        {
            using (SqlConnection con = new SqlConnection(TestDataAccessLayer.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("spUpdateTest", con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@RaceDescription", raceID);
                    cmd.Parameters.AddWithValue("@CountyName", countyId);
                    cmd.Parameters.AddWithValue("@Gender", gender);
                    cmd.Parameters.AddWithValue("@DateOfBirth", dateOfBirth);
                    cmd.Parameters.AddWithValue("@SesDescription", sesId);
                    cmd.Parameters.AddWithValue("@PatientId", PatientId);
                    cmd.ExecuteNonQuery();
                }
            }
        }

this works 这很有效

public static void DeleteTest(int PatientId)
        {
            using (SqlConnection con = new SqlConnection(TestDataAccessLayer.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("spDeleteTest", con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@PatientId", PatientId);
                    cmd.ExecuteNonQuery();
                }
            }
        }

Where is this mysterious extra parameter coming from that I don't know about? 这个神秘的额外参数来自我不知道的地方?

Your parameter names in your C# code do not match what is defined in the stored procedure. C#代码中的参数名称与存储过程中定义的参数名称不匹配。

Here are the names that do not match what is in the stored procedure: 以下是与存储过程中的名称不匹配的名称:

  • @RaceId - it is @RaceDescription in the C# code @RaceId - 它是C#代码中的@RaceDescription
  • @CountyId - it is @CountryName in the C# code @CountyId - 它是C#代码中的@CountryName
  • @SesId - it is @SesDescription in the C# code @SesId - 它是C#代码中的@SesDescription

My guess, without seeing the code, for your delete stored procedure, is that the @PatientId parameters matches on both sides (C# and stored procedure) so that is why that one works and the update does not. 我的猜测,没有看到代码,对于你的删除存储过程,是@PatientId参数在两侧都匹配(C#和存储过程),这就是为什么那个工作而更新没有。

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

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