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


有什么建议么?

最佳答案

我会尝试明确指定参数的类型-如果该值为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或类似的东西吗?

10-02 01:26