gpt4 book ai didi

sql - 存储过程或函数需要未提供的参数

转载 作者:行者123 更新时间:2023-12-01 18:55:10 25 4
gpt4 key购买 nike

我尝试通过调用存储过程将数据插入 SQL Server 数据库,但收到错误

Procedure or function 'SHOWuser' expects parameter '@userID', which was not supplied.

我的存储过程称为SHOWuser。我已经仔细检查过,没有缺少任何参数。

我的代码是:

public void SHOWuser(string userName, string password, string emailAddress, List<int> preferences)
{
SqlConnection dbcon = new SqlConnection(conn);

try
{
SqlCommand cmd = new SqlCommand();

cmd.Connection = dbcon;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "SHOWuser";

cmd.Parameters.AddWithValue("@userName", userName);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@emailAddress", emailAddress);

dbcon.Open();

int i = Convert.ToInt32(cmd.ExecuteScalar());

cmd.Parameters.Clear();
cmd.CommandText = "tbl_pref";

foreach (int preference in preferences)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@userID", Convert.ToInt32(i));
cmd.Parameters.AddWithValue("@preferenceID", Convert.ToInt32(preference));

cmd.ExecuteNonQuery();
}
}
catch (Exception)
{
throw;
}
finally
{
dbcon.Close();
}

存储过程是:

ALTER PROCEDURE [dbo].[SHOWuser]
(
@userName varchar(50),
@password nvarchar(50),
@emailAddress nvarchar(50)
)
AS
BEGIN
INSERT INTO tbl_user(userName, password, emailAddress)
VALUES (@userName, @password, @emailAddress)

SELECT
tbl_user.userID, tbl_user.userName,
tbl_user.password, tbl_user.emailAddress,
STUFF((SELECT ',' + preferenceName
FROM tbl_pref_master
INNER JOIN tbl_preferences ON tbl_pref_master.preferenceID = tbl_preferences.preferenceID
WHERE tbl_preferences.userID = tbl_user.userID
FOR XML PATH ('')), 1, 1, ' ' ) AS Preferences
FROM
tbl_user

SELECT SCOPE_IDENTITY();
END

这是在同一函数中使用的第二个存储过程tbl_pref:

ALTER PROCEDURE [dbo].[tbl_pref]
@userID int,
@preferenceID int
AS
BEGIN
INSERT INTO tbl_preferences(userID, preferenceID)
VALUES (@userID, @preferenceID)
END

最佳答案

就我而言,即使正确提供了所有参数值但未指定命令类型,我也会收到此异常:

cmd.CommandType = System.Data.CommandType.StoredProcedure;

这显然不是上面问题中的情况,但在这种情况下异常描述不是很清楚,所以我决定具体说明。

关于sql - 存储过程或函数需要未提供的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703653/

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