OpenSqlConnection();
SqlCommand cmd = new SqlCommand("ReturnCharacterInfo", Con);
cmd.Parameters.AddWithValue("@CharacterID", CharacterID);
SqlDataReader dr = cmd.ExecuteReader();
当我运行代码时,您可以看到上面的内容,它会抛出异常
过程或函数'ReturnCharacterInfo'需要参数'@CharacterID',这是未提供的。
但正如您在代码中看到的那样,我正在使用@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
有什么建议吗?
答案 0 :(得分:2)
我会尝试显式指定参数的类型 - 如果值为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
或类似内容?
答案 1 :(得分:1)
正如CodeNaked所指出的,您需要设置SqlCommand的命令类型。由于存储过程的执行方式,需要告诉SqlCommand对象是存储过程。
OpenSqlConnection();
SqlCommand cmd = new SqlCommand("ReturnCharacterInfo", Con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CharacterID", CharacterID);
SqlDataReader dr = cmd.ExecuteReader();