2
OpenSqlConnection();

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

SqlDataReader dr = cmd.ExecuteReader();

当我运行您可以在上面看到的代码时,它会引发异常

过程或函数“ReturnCharacterInfo”需要未提供的参数“@CharacterID”。

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

存储过程:

    [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

有什么建议么?

4

2 回答 2

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在你的代码中某处有类似的东西?

于 2012-11-14T19:18:23.677 回答
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();
于 2012-11-14T19:25:03.043 回答