【发布时间】:2018-07-12 19:35:00
【问题描述】:
我打开我的SqlConnection,然后使用 foreach 循环和我的存储过程来收集数据。我使用Parameters.AddWithValue 将数据发送到我的存储过程中,但我一直收到错误
System.Data.SqlClient.SqlException: '过程或函数 spInsertLeafPickup 指定的参数过多。
有什么理由吗?
存储过程:
CREATE TYPE Templeafpickup AS TABLE
(
ID INT PRIMARY KEY,
Address NVARCHAR(50),
SeasonType NVARCHAR(50),
NextPickupdate NVARCHAR(10)
)
CREATE PROCEDURE spInsertLeafPickup
@Templeafpickup Templeafpickup READONLY
AS
BEGIN
INSERT INTO LeafPickup([ID], [Address], [SeasonType], [NextPickupdate])
SELECT
[ID], [Address], [SeasonType], [NextPickupdate]
FROM
@Templeafpickup
END
C#类:
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(cs))
{
con1.Open();
foreach (DataRow dr1 in dt.Rows)
{
SqlCommand cmd = new SqlCommand("spInsertLeafPickup", con1);
cmd.CommandType = CommandType.StoredProcedure;
//Insert stored procedure
cmd.Parameters.AddWithValue("@Address", dr1["PCOMBINED"]);
cmd.Parameters.AddWithValue("@SeasonType", dr1["PSSSTREET"]);
cmd.Parameters.AddWithValue("@NextPickupdate", dr1["ZST"]);
cmd.ExecuteNonQuery(); <= Error System.Data.SqlClient.SqlException: 'Procedure or function spInsertLeafPickup has too many arguments specified.'
}
con1.Close();
}
【问题讨论】:
-
可以在foreach循环之外定义命令和参数。然后只需设置循环中每个参数的值即可。
-
错误并没有更清楚。我们还想怎么向你解释???在您的 sp 定义中,您只有 1 个参数。但是您在代码中传递了 3。
标签: sql-server stored-procedures parameters sqlconnection