C# 存储过程的参数太多

C# 存储过程的参数太多,c#,asp.net,sql,gridview,stored-procedures,C#,Asp.net,Sql,Gridview,Stored Procedures,可复制 我正在使用SqlDataSource控件在GridView中实现一些更新和删除功能。我不断收到运行时错误过程或函数spUpdateTest指定的参数太多。在SQL Server中运行存储过程时,它工作正常。当我单击GridView的delete部分时,它就工作了。然而,当我尝试更新时,我得到了上面的错误 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<

可复制


我正在使用SqlDataSource控件在GridView中实现一些更新和删除功能。我不断收到运行时错误
过程或函数spUpdateTest指定的参数太多。
在SQL Server中运行存储过程时,它工作正常。当我单击GridView的delete部分时,它就工作了。然而,当我尝试更新时,我得到了上面的错误

 <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>
两者都有六个参数。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>
这很有效

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();
                }
            }
        }

我不知道这个神秘的额外参数从何而来?

您的C代码中的参数名称与存储过程中定义的不匹配

以下是与存储过程中的名称不匹配的名称:

  • @RaceId-它是C代码中的
    @racescription
  • @CountyId-在C代码中是
    @CountryName
  • @SesId-它是C代码中的
    @sessdescription

我猜,在没有看到代码的情况下,对于delete存储过程,
@PatientId
参数在两个方面都匹配(C#和存储过程),这就是为什么该参数有效而更新无效的原因。

以后,使用Profiler查找发送的确切代码。通常,一旦您准确地看到发送的内容,您就可以找出问题所在。这个特定于SQl Server但其他数据库具有某种分析功能,这些功能随附,或者您可以获得。在没有软件的情况下,切勿针对数据库编写代码,该软件允许您分析应用程序实际发送到数据库的内容。这将为您节省数千个调试小时。@HLGEM这就是我要寻找的建议类型:实用。问题是GridView控件中的所有内容都是绑定字段,因此被转换为存储过程的参数。我有一个字段是
GetDate()
,没有使用。因此,我在存储过程中添加了一个默认值为null的额外参数,它按照预期工作。
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();
                }
            }
        }
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();
                }
            }
        }