【问题标题】:Procedure or function *** has too many arguments specified过程或函数 *** 指定的参数过多
【发布时间】:2014-02-08 20:53:59
【问题描述】:

每当我尝试将某些内容插入数据库时​​,都会收到此错误:

过程或函数 sp_Customer_Insert 指定的参数过多。

尝试在线搜索帮助,但找不到任何有用的信息。

编辑:我从插入参数中删除了p_cust_id,但它仍然给出相同的错误,请帮助我真的不明白我做错了什么

存储过程:

ALTER PROCEDURE [dbo].[sp_Customer_Insert]
 @p_cust_name nvarchar(50)
,@p_cust_address nvarchar(50)
,@p_cust_city nvarchar(50)
,@p_cust_state nvarchar(5)
,@p_cust_zip nvarchar(10)
,@p_cust_country nvarchar(50)
,@p_cust_contact nvarchar(50)
,@p_cust_email  nvarchar(255)
,@p_cust_birthday datetime
AS
BEGIN

INSERT INTO customers
       (cust_name
       ,cust_address
       ,cust_city
       ,cust_state
       ,cust_zip
       ,cust_country
       ,cust_contact
       ,cust_email
       ,cust_birthday)
 VALUES
       (@p_cust_name
       ,@p_cust_address
       ,@p_cust_city 
       ,@p_cust_state
       ,@p_cust_zip
       ,@p_cust_country
       ,@p_cust_contact
       ,@p_cust_email
       ,@p_cust_birthday
       );
END;

ASP 代码:

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" Height="50px" Width="223px" AutoGenerateRows="False" DataKeyNames="cust_id">
        <Fields>
            <asp:BoundField DataField="cust_id" HeaderText="cust_id" InsertVisible="False" ReadOnly="True" SortExpression="cust_id" />
            <asp:BoundField DataField="cust_name" HeaderText="cust_name" SortExpression="cust_name" />
            <asp:BoundField DataField="cust_address" HeaderText="cust_address" SortExpression="cust_address" />
            <asp:BoundField DataField="cust_city" HeaderText="cust_city" SortExpression="cust_city" />
            <asp:BoundField DataField="cust_state" HeaderText="cust_state" SortExpression="cust_state" />
            <asp:BoundField DataField="cust_zip" HeaderText="cust_zip" SortExpression="cust_zip" />
            <asp:BoundField DataField="cust_country" HeaderText="cust_country" SortExpression="cust_country" />
            <asp:BoundField DataField="cust_contact" HeaderText="cust_contact" SortExpression="cust_contact" />
            <asp:BoundField DataField="cust_email" HeaderText="cust_email" SortExpression="cust_email" />
            <asp:BoundField DataField="cust_birthday" HeaderText="cust_birthday" SortExpression="cust_birthday" />
            <asp:CommandField ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CrashCourseConnectionString %>" InsertCommand="sp_Customer_Insert" InsertCommandType="StoredProcedure" SelectCommand="sp_CustomerByID" SelectCommandType="StoredProcedure">
        <InsertParameters>
            <asp:Parameter Name="p_cust_name" Type="String" />
            <asp:Parameter Name="p_cust_address" Type="String" />
            <asp:Parameter Name="p_cust_city" Type="String" />
            <asp:Parameter Name="p_cust_state" Type="String" />
            <asp:Parameter Name="p_cust_zip" Type="String" />
            <asp:Parameter Name="p_cust_country" Type="String" />
            <asp:Parameter Name="p_cust_contact" Type="String" />
            <asp:Parameter Name="p_cust_email" Type="String" />
            <asp:Parameter Name="p_cust_birthday" Type="DateTime" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlCustomers" Name="cust_id" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

C#

public static DataTable InsertCustomer(string Name, string Address, string City, string State, string Zip, string Country, string Contact, string Email, DateTime BDay)
{
        DataTable DT = new DataTable();

        SqlDataAdapter DA = new SqlDataAdapter("sp_Customer_Insert", CData.CData.GetConnection());

        DA.SelectCommand.CommandType = CommandType.StoredProcedure;

        DA.SelectCommand.Parameters.AddWithValue("@p_cust_name", Name);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_address", Address);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_city", City);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_state", State);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_zip", Zip);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_country", Country);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_contact", Contact);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_email", Email);
        DA.SelectCommand.Parameters.AddWithValue("@p_cust_birthday", BDay);

        return DT;

有人可以帮忙吗?

我看不出我做错了什么。

【问题讨论】:

  • 您不能使用// 来评论aspx 代码。请改用&lt;!-- &lt;asp:Parameter Name="p_cust_id" Type="Int32" /&gt; --&gt;,或者直接删除该行,因为无论如何都不需要它。
  • 我彻底删除了。还是一样的错误
  • 旁注:您应该为您的存储过程使用sp_ 前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好直接避免 sp_ 并使用其他东西作为前缀 - 或者根本不使用前缀!
  • 不清楚InsertCustomer 方法在做什么。对于插入 SP,您添加的是 SelectCommand.Parameters 而不是 InsertCommand.Parameters,并且最后没有插入。创建 null DataTable 并返回它。你真的在用这个吗?

标签: c# asp.net sql sql-server stored-procedures


【解决方案1】:

我有一个类似的问题,原来是由于使用了DataKeyName,它没有用作我的(更新)存储过程的参数。尽管我找不到任何文档来解释原因,但它似乎包含在更新参数集合中。我当时通过在存储过程中添加 DataKey 作为(未使用的)参数来纠正问题。我不需要添加到updateParameters 集合中。

这只是一个临时修复,以测试这是否能解决问题 - 最后我重新编码了页面,因此我没有使用 DataKey。

在这种情况下,我会尝试将cust_id 的声明添加到sp_Customer_Insert

【讨论】:

    【解决方案2】:

    由于sp_Customer_Insert中没有@p_cust_id参数,所以需要将&lt;asp:Parameter Name="p_cust_id" Type="Int32" /&gt;&lt;InsertParameters&gt;中删除:

    <InsertParameters>
        <asp:Parameter Name="p_cust_name" Type="String" />
        <asp:Parameter Name="p_cust_address" Type="String" />
        <asp:Parameter Name="p_cust_city" Type="String" />
        <asp:Parameter Name="p_cust_state" Type="String" />
        <asp:Parameter Name="p_cust_zip" Type="String" />
        <asp:Parameter Name="p_cust_country" Type="String" />
        <asp:Parameter Name="p_cust_contact" Type="String" />
        <asp:Parameter Name="p_cust_email" Type="String" />
        <asp:Parameter Name="p_cust_birthday" Type="DateTime" />
    </InsertParameters>
    

    【讨论】:

      【解决方案3】:

      当我们尝试插入比我们定义的更多的参数时,我们会收到此错误

      在您的 .aspx 页面代码中,您定义了一个额外的参数,即

      <asp:Parameter Name="p_cust_id" Type="Int32" />
      

      删除此行,因为您没有插入 id 字段。

      【讨论】:

        【解决方案4】:

        您需要从 ASP 文件中的 &lt;InsertParameters&gt; 中删除 &lt;asp:Parameter Name="p_cust_id" Type="Int32" /&gt;

        【讨论】:

          猜你喜欢
          • 2023-03-26
          • 2015-12-10
          • 2016-02-22
          • 1970-01-01
          相关资源
          最近更新 更多