从C#Windows应用程序中的存储过程中检索值

时间:2011-10-18 15:18:11

标签: c# stored-procedures

我试图在C#WPF应用程序中显示存储过程返回的值。用户输入卡号,单击“提交”并运行以下代码:

if (textBox1.Text != "")
{
    long numCardNumber; //Card number (card number scanned and converted to a number)
    string strCardNumber; // Card number (card number stored as string)
    char[] MyChar = {';','?'};

    label2.Text = textBox1.Text.TrimEnd(MyChar); // Trims end
    label2.Text = label2.Text.TrimStart(MyChar); //Trims beginning

    strCardNumber = label2.Text; 

    try //try and convert the string to a number (if valid numerical characters
    {
        numCardNumber = Convert.ToInt64(strCardNumber);
        label1.Text = "Your number is: " + label2.Text; // Scanned " + numOfTimeScanned + " times"; 


    }
    catch(FormatException) // thrown if input characters are not valid numeric
    {
        label1.Text = "NOT A VALID CARD INPUT!";
    }

    SqlConnection connection = new SqlConnection("Data Source=TestServer;Initial Catalog=Testdb;Persist Security Info=True;User ID=testuser;Password=testpass");

    connection.Open();
    SqlCommand cmd = new SqlCommand("GW_MAGTOOCR", connection);
    cmd.CommandType = CommandType.StoredProcedure;

    SqlParameter param = new SqlParameter("@iMAG", SqlDbType.Char);
    param.Value = label1.Text;
    cmd.Parameters.Add(param);

    SqlParameter retval = cmd.Parameters.Add("@iMAG", SqlDbType.VarChar);
    retval.Direction = ParameterDirection.Output;

    int returnvalue = (int)cmd.Parameters["@iMAG"].Value;


} //END IF
else
{
    label1.Text = "Nothing Submitted!";
}

以下是涉及的2个存储过程。应该返回一个整数

从代码调用的名为GW_MAGTOOCR的存储过程1(反过来,调用称为CDS_INTTOHEX的存储过程2)。

已存储的程序1:

USE [Testdb]
GO
/****** Object:  StoredProcedure [dbo].[GW_MAGTOOCR]  */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER procedure [dbo].[GW_MAGTOOCR](
  @iMag char(18),
  @oOCR varchar(10) output
)
--WITH ENCRYPTION
as

declare
  @vTStr   varchar(9),
  @vPrefix char(2),
  @vCRC    varchar(2),
  @vTemp   int
begin
  IF isnumeric(@iMag) = 0
    Begin
      Set @oOcr = Null
      Return
    End
  -- get the prefix
  select @vTemp = convert(int, substring(@iMag, 4, 3))
  execute CDS_INTTOHEX @vTemp, @vTStr output
  select @vPrefix = substring(@vTStr, 7, 2)
  -- get the CRC
  select @vTemp = convert(int, substring(@iMag, 7, 2))
  execute CDS_INTTOHEX @vTemp, @vTStr output
  select @vCRC = substring(@vTStr, 8, 1)
  select @vTemp = convert(int, substring(@iMag, 9, 2))
  execute CDS_INTTOHEX @vTemp, @vTStr output
  select @vCRC = @vCRC + substring(@vTStr, 8, 1)
  -- get the account #
  select @vTemp = convert(int, substring(@iMag, 11, 8))
  execute CDS_INTTOHEX @vTemp, @vTStr output
  select @oOCR = @vPrefix + @vCRC + substring(@vTStr, 3, 6)
 /*select output = @vPrefix + @vCRC + substring(@vTStr, 3, 6)*/
end

已存储的程序2:

USE [Testdb]
GO
/****** Object:  StoredProcedure [dbo].[CDS_INTTOHEX]  *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[CDS_INTTOHEX] (
  @iInteger   int,
  @oHex       varchar(9) output
) 
--WITH ENCRYPTION
as begin

  declare
    @vDec       int,
    @vHex       char,
    @vLen       int,
    @vDivisor   int,
    @vQuotient  int,
    @vRemainder int,
    @vTemp      varchar(9)
  select @vTemp = ''
  select @vQuotient = @iInteger
  select @oHex      = ''
  select @vLen      = 8
  while  @vLen > 0 begin
    select  @vLen = @vLen - 1
    select  @vDivisor = power(16, @vLen)
    select  @vDec = floor(@vQuotient / @vDivisor)
    if      @vDec < 10 select @vHex =convert(char, @vDec)
    else if @vDec = 10 select @vHex = 'A'
    else if @vDec = 11 select @vHex = 'B'
    else if @vDec = 12 select @vHex = 'C'
    else if @vDec = 13 select @vHex = 'D'
    else if @vDec = 14 select @vHex = 'E'
    else if @vDec = 15 select @vHex = 'F'
    select @vTemp = @vTemp + @vHex
    select  @vQuotient = @vQuotient - (@vDec * @vDivisor)
  end
  select @oHex = @vTemp
end

任何帮助都会非常感激。我很确定问题出在我的C#代码中,因为我可以从SQL管理工作室成功调用存储过程并收回预期的值。

修改

我已经更新了我的代码,我创建了一个简单的存储过程,它基本上调用了前两个(最初的MAGTOOCR),当我从Management Studio运行它时似乎运行正常,现在我只是不确定如果我的代码正确调用它,这是我更新的代码:

connection.Open();
            SqlCommand cmd = new SqlCommand("CNBID", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter param = cmd.Parameters.Add("@iMAG", SqlDbType.Char, 18); 
            param.Direction = ParameterDirection.Output;
            param.Value = label2.Text;

            SqlDataReader rdr = cmd.ExecuteReader();

            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    label2.Text = "Reading";
                }
            }
            else
            {
                label2.Text = "NOTHING READ!";
            }

我知道代码最后没有输出任何有用的东西,但我想在调试时观察变量。我确定我上面没有做正确的事情。任何帮助再次受到赞赏。

1 个答案:

答案 0 :(得分:3)

如果您希望该过程输出@iMAG的值,则需要将参数方向设置为Output

SqlParameter parm = cmd.Parameters.Add( "@iMAG", SqlDbType.Char);
parm.Direction = ParameterDirection.Output;

修改

您在代码中两次创建@iMAG参数。只需创建一次,如下所示:

SqlParameter param = cmd.Parameters.Add("@iMAG", SqlDbType.VarChar); 
param.Direction = ParameterDirection.Output
param.Value = label1.Text;