gpt4 book ai didi

c# - 错误代码 : procedure has too many arguments specified

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:53 24 4
gpt4 key购买 nike

我有一个 SQL Server 存储过程。当我执行它时,出现此错误:

SQL Server procedure has too many arguments specified

我该如何解决这个问题?代码如下所示。

当我的存储过程运行时,如果记录存在,我需要更新然后添加记录。

SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Insert_MS]
(@SID char(20),
@CREATE_DATETIME char(14),
@MODIFY_DATETIME char(14),
@CREATOR_SID char(20),
@MODIFIER_SID char(20),
@MARK_DELETED char(1),
@TARGET_SID nvarchar(20),
@TARGET_CODE nvarchar(50),
@UNIT nvarchar(100),
@SPECIFICATION nvarchar(1000),
@MATERIALS_SUBCAT nvarchar(1000),
@SORT int,
@SET_UNIT nvarchar(1000),
@ENABLED char(1),
@MARKET_TYPE nvarchar(2)
)
AS
BEGIN
IF EXISTS (SELECT *
FROM [dbo].[M_S]
WHERE [MARKET_TYPE] = @MARKET_TYPE
AND [TARGET_CODE] = @TARGET_CODE
AND [TARGET_SID] = @TARGET_SID)
BEGIN
--update existing record
UPDATE [dbo].[M_S]
SET [MATERIALS_SUBCAT] = @MATERIALS_SUBCAT ,
[SPECIFICATION] = @SPECIFICATION,
[UNIT] = @UNIT
WHERE [MARKET_TYPE] = @MARKET_TYPE
AND [TARGET_CODE] = @TARGET_CODE
AND [TARGET_SID] = @TARGET_SID
END
ELSE
BEGIN
--insert new record
INSERT INTO [dbo].[M_S] ([SID], [CREATE_DATETIME], [MODIFY_DATETIME],
[CREATOR_SID], [MODIFIER_SID], [MARK_DELETED],
[TARGET_SID], [TARGET_CODE], [UNIT], [SPECIFICATION],
[MATERIALS_SUBCAT], [SORT], [SET_UNIT],
[ENABLED], [MARKET_TYPE])
VALUES (@SID, @CREATE_DATETIME, @MODIFY_DATETIME,
@CREATOR_SID, @MODIFIER_SID, @MARK_DELETED,
@TARGET_SID, @TARGET_CODE, @UNIT,
@SPECIFICATION,
@MATERIALS_SUBCAT, @SORT, @SET_UNIT,
@ENABLED, @MARKET_TYPE)
END
END

那我的aspx.cs代码就在这里

cnn.Open();

insertSql += " INSERT INTO [dbo].[MATERIALS_SUBCAT] ([SID],[CREATE_DATETIME], [MODIFY_DATETIME],";
insertSql += " [CREATOR_SID], [MODIFIER_SID], [MARK_DELETED],[TARGET_SID], [TARGET_CODE],";
insertSql += " [UNIT], [SPECIFICATION], [MATERIALS_SUBCAT], [SORT],[SET_UNIT], [ENABLED], [MARKET_TYPE])";
insertSql += " VALUES (@SID, @CREATE_DATETIME, @MODIFY_DATETIME, @CREATOR_SID, @MODIFIER_SID, @MARK_DELETED, @TARGET_SID, @TARGET_CODE, @UNIT, @SPECIFICATION,";
insertSql += "@MATERIALS_SUBCAT, @SORT, @SET_UNIT, @ENABLED, @MARKET_TYPE) ";

SqlCommand cmdStoredProcedure = new SqlCommand("Insert_MS", cnn);
cmdStoredProcedure.CommandType = CommandType.StoredProcedure;

for (int k = 0; k <= dt_sheet.Rows.Count - 1; k++)
{
cmdStoredProcedure.Parameters.AddWithValue("@SID", dt_sheet.Rows[k]["SID"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@CREATE_DATETIME", dt_sheet.Rows[k]["CREATE_DATETIME"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@MODIFY_DATETIME", dt_sheet.Rows[k]["MODIFY_DATETIME"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@CREATOR_SID", dt_sheet.Rows[k]["CREATOR_SID"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@MODIFIER_SID", dt_sheet.Rows[k]["MODIFIER_SID"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@MARK_DELETED", dt_sheet.Rows[k]["MARK_DELETED"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@TARGET_SID", dt_sheet.Rows[k]["分類代碼"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@TARGET_CODE", dt_sheet.Rows[k]["品項代碼"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@UNIT", dt_sheet.Rows[k]["單位"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@SPECIFICATION", dt_sheet.Rows[k]["規格"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@MATERIALS_SUBCAT", dt_sheet.Rows[k]["品項名稱"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@SORT", dt_sheet.Rows[k]["SORT"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@SET_UNIT",null);
cmdStoredProcedure.Parameters.AddWithValue("@ENABLED", dt_sheet.Rows[k]["ENABLED"].ToString());
cmdStoredProcedure.Parameters.AddWithValue("@MARKET_TYPE", dt_sheet.Rows[k]["MARKET_TYPE"].ToString());

cmdStoredProcedure.ExecuteNonQuery();
cnn.Close();
}

最佳答案

似乎您想在每次遍历工作表时都执行存储过程。将命令移动到循环中并使用“使用”来轻松处理每次命令,如下所示:

 for (int k = 0; k <= dt_sheet.Rows.Count - 1; k++)
{
using (SqlCommand cmdStoreProcedure = new SqlCommand("Insert_MS", cnn))
{
cmdStoreProcedure.CommandType = CommandType.StoredProcedure;
cmdStoreProcedure.Parameters.AddWithValue("@SID", dt_sheet.Rows[k]["SID"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@CREATE_DATETIME", dt_sheet.Rows[k]["CREATE_DATETIME"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@MODIFY_DATETIME", dt_sheet.Rows[k]["MODIFY_DATETIME"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@CREATOR_SID", dt_sheet.Rows[k]["CREATOR_SID"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@MODIFIER_SID", dt_sheet.Rows[k]["MODIFIER_SID"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@MARK_DELETED", dt_sheet.Rows[k]["MARK_DELETED"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@TARGET_SID", dt_sheet.Rows[k]["分類代碼"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@TARGET_CODE", dt_sheet.Rows[k]["品項代碼"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@UNIT", dt_sheet.Rows[k]["單位"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@SPECIFICATION", dt_sheet.Rows[k]["規格"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@MATERIALS_SUBCAT", dt_sheet.Rows[k]["品項名稱"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@SORT", dt_sheet.Rows[k]["SORT"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@SET_UNIT", null);
cmdStoreProcedure.Parameters.AddWithValue("@ENABLED", dt_sheet.Rows[k]["ENABLED"].ToString());
cmdStoreProcedure.Parameters.AddWithValue("@MARKET_TYPE", dt_sheet.Rows[k]["MARKET_TYPE"].ToString());
// connection.Open();
cmdStoreProcedure.ExecuteNonQuery();
}
}
cnn.Close();

关于c# - 错误代码 : procedure has too many arguments specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com