gpt4 book ai didi

c# - 无法提供存储过程参数

转载 作者:行者123 更新时间:2023-11-30 22:23:26 25 4
gpt4 key购买 nike

OpenSqlConnection();

SqlCommand cmd = new SqlCommand("ReturnCharacterInfo", Con);
cmd.Parameters.AddWithValue("@CharacterID", CharacterID);

SqlDataReader dr = cmd.ExecuteReader();

当我运行上面的代码时,它会抛出异常

Procedure or function 'ReturnCharacterInfo' expects parameter '@CharacterID', which was not supplied.

但是正如您在代码中看到的那样,我正在提供 @CharacterID 参数,它不是空的或 null。

存储过程:

    [ReturnCharacterInfo] @CharacterID int 
as
select
CharacterName, characterSurname, CharacterIsMale, CharacterAge,
CharacterMood, CharacterHealth, CharacterInCity, CharacterInLocation,
Cities.CityName, Locales.LocaleTitle
from
Characters
INNER JOIN
Cities on Cities.CityID = Characters.CharacterInCity
INNER JOIN
Locales on Locales.LocaleID = Characters.CharacterInLocation
Where
CharacterID = @CharacterID

有什么建议吗?

最佳答案

我会尝试明确指定参数的类型 - 如果值为 null 则尤为重要:

SqlCommand cmd = new SqlCommand("ReturnCharacterInfo", Con);

// added as per CodeNaked's comment - thanks! You've nailed it right on the head!
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@CharacterID", SqlDbType.Int).Value = CharacterID;

还有:你有没有错别字?? 三次检查所有出现的CharacterID - 你的代码中是否碰巧有CharachterID或类似的东西??

关于c# - 无法提供存储过程参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13385697/

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