从数据库中检索字符串值时出现此错误。我正在转换为字符串值。
此代码在VS 2005和VS 2008中有效,但是当我尝试在VS 2017中运行它时,出现错误。
这是代码
private void GetExplanation(Card c, int position)
{
SqlCommand cmd = new SqlCommand
{
CommandText = "usp_PCGetExplanation"
};
cmd.Parameters.Add("@positionID", SqlDbType.VarChar);
cmd.Parameters.Add("@cardNumber", SqlDbType.Int);
cmd.Parameters["@positionID"].Value = position;
cmd.Parameters["@cardNumber"].Value = c.CardValue + 1;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
try
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
if (rdr.HasRows)
{
c.Explanation = rdr.GetString(0);
}
else
{
MessageBox.Show("Unable to get explanation");
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format(
"Error retrieving explanation {0}", ex.Message));
}
finally
{
conn.Close();
cmd.Dispose();
}
}
这是 Card 类属性
public class Card
{
public static string[] faceValues = {"Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
public static string[] suits = { "Hearts", "Clubs", "Diamonds", "Spades" };
private int cardValue;
private string faceName;
private string suitName;
private string explanation;
public bool isNegative;
private Image cardPicture;
private readonly Random rnd = new Random(52);
public Card(int value)
{
cardValue = value;
faceName = faceValues[(value) % 13];
suitName = suits[value / 13];
explanation = " ";
}
public int CardValue
{
get { return cardValue; }
set { cardValue = value; }
}
public string FaceName
{
get { return faceName; }
set { faceName = value; }
}
public string SuitName
{
get { return suitName; }
set { suitName = value; }
}
public string Explanation
{
get { return explanation; }
set { explanation = value; }
}
public Image CardPicture
{
get { return cardPicture; }
set { cardPicture = value; }
}
public int FaceValue
{
get { return cardValue % 13; }
}
public override string ToString()
{
return string.Format("{0} of {1}", FaceName, suitName);
}
public bool IsNegative
{
get
{
if ( suitName == "spades")
{
return true;
}
else
{
return false;
}
}
}
}
}
这是存储过程
USE [PersonalForecast]
GO
/****** Object: StoredProcedure [dbo].[usp_PCGetExplanation] Script Date: 3/21/2019 5:28:16 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Tom Magaro
-- Create date: 20101214
-- Description: Gets the PositionCard row
-- =============================================
ALTER PROCEDURE [dbo].[usp_PCGetExplanation]
-- Add the parameters for the stored procedure here
@positionId int,
@cardNumber int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT Explanation
FROM PositionCard
WHERE PositionId = @positionId and
CardId = @cardNumber
ORDER BY positionId, CardId
END
任何对此的帮助将不胜感激
汤姆
答案 0 :(得分:1)
我认为问题出在您的参数类型上。 @positionID
可能是int,因为您的参数在存储过程中是int
cmd.Parameters.Add("@positionID", SqlDbType.Int);
cmd.Parameters.Add("@cardNumber", SqlDbType.Int);
c#参数类型是存储过程参数类型上的匹配项。
答案 1 :(得分:-1)
错误出在存储过程中。
@positionId int = 0, @cardValue int = 0
应该是
@positionId int, @cardValue int