为什么在此存储过程中需要输出参数?

时间:2014-11-14 03:18:47

标签: c# winforms stored-procedures

我编写了一个包含一些输出参数的简单存储过程,但是当我从C#代码执行它时,出现错误

  

预期参数

期待我提供OUTPUT参数的值......

请帮我弄清楚错误...

我的存储过程:

ALTER PROCEDURE [dbo].[Check_EntryTrade_value] 
    @Account_ID bigint,
    @Date datetime2(7),
    @tradeby varchar(20),
    @Symbol varchar(20),
    @B_S varchar(20),
    @RateE float ,
    @Usd_Marg bigint OUTPUT,
    @Amount bigint OUTPUT,
    @Rate float OUTPUT,
    @Entry_Order_id bigint OUTPUT
AS
BEGIN
    SELECT 
        @Rate = [Rate], 
        @Entry_Order_id = [EntryOrder_ID],
        @Amount = [Amount],
        @Usd_Marg = [Usd_Mrg]  
    FROM  
        [dbo].[entrytable] 
    WHERE
        [B_S]  = @B_S 
        AND [Rate] = @RateE 
        AND [Account_ID] = @Account_ID 
        AND [Symbol] = @Symbol;

    IF (@Rate IS NOT NULL)
       INSERT INTO neworder (Account_ID, Symbol, B_S, Rate, Amount, Date, Usd_Marg, tradeby) 
       VALUES(@Account_ID, @Symbol, @B_S, @Rate, @Amount, @Date, @Usd_Marg, @tradeby);

    DELETE FROM entrytable WHERE EntryOrder_ID = @Entry_Order_id;
END    

这是我的C#代码:

SqlConnection conn = new SqlConnection(Properties.Settings.Default.MyCon);
conn.Open();

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;

//SQL INJECTION '"+ +"'
cmd.CommandText = "Check_EntryTrade_value";
SqlParameter pID1 = new SqlParameter("@Account_ID", SqlDbType.BigInt, 10);
pID1.Value = 1;
SqlParameter pID2 = new SqlParameter("@Symbol", SqlDbType.VarChar, 20);
pID2.Value = "EUR/USD";
SqlParameter pID3 = new SqlParameter("@B_S", SqlDbType.VarChar, 20);
pID3.Value = "Buy";
SqlParameter pID4 = new SqlParameter("@RateE", SqlDbType.Float, 10);
pID4.Value = 1.24763;
//SqlParameter pID6 = new SqlParameter("@Amount", SqlDbType.BigInt, 10);
//pID6.Value = 1;
SqlParameter pID5 = new SqlParameter("@Date", SqlDbType.DateTime2, 7);
pID5.Value = DateTime.Now.ToShortDateString();
SqlParameter pID6 = new SqlParameter("@tradeby", SqlDbType.VarChar, 20);
pID6.Value = "User";

cmd.Parameters.Add(pID1);
cmd.Parameters.Add(pID2);
cmd.Parameters.Add(pID3);
cmd.Parameters.Add(pID4);
cmd.Parameters.Add(pID5);
cmd.Parameters.Add(pID6);

cmd.ExecuteNonQuery();      

运行我的C#代码会返回以下错误:

  

程序或功能' Check_EntryTrade_value'期望参数' @ Usd_Marg',这是未提供的。

2 个答案:

答案 0 :(得分:1)

我找到了它:)感谢每一个人的帮助..我喜欢StackOverFlow:P

这就是我所做的:

//STEP-1 CONNECTION
SqlConnection conn = new SqlConnection(Properties.Settings.Default.MyCon);
  //STEP-2 COmmand
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
//SQL INJECTION '"+ +"'
cmd.CommandText = "Check_EntryTrade_value";


SqlParameter pID1 = new SqlParameter("@Account_ID", SqlDbType.BigInt, 10);
pID1.Value = 1;
SqlParameter pID2 = new SqlParameter("@Symbol", SqlDbType.VarChar, 20);
pID2.Value = "EUR/USD";
SqlParameter pID3 = new SqlParameter("@B_S", SqlDbType.VarChar, 20);
pID3.Value = "Buy";
SqlParameter pID4 = new SqlParameter("@RateE", SqlDbType.Float, 10);
pID4.Value = 1.24763;
//SqlParameter pID6 = new SqlParameter("@Amount", SqlDbType.BigInt, 10);
//pID6.Value = 1;
SqlParameter pID5 = new SqlParameter("@Date", SqlDbType.DateTime2, 7);
pID5.Value = DateTime.Now.ToShortDateString();
SqlParameter pID6 = new SqlParameter("@tradeby", SqlDbType.VarChar, 20);
pID6.Value = "User";

SqlParameter pID7 = new SqlParameter("@Usd_Marg", SqlDbType.BigInt, 10);
pID7.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID8 = new SqlParameter("@Amount", SqlDbType.BigInt, 20);
pID8.Direction = ParameterDirection.Output;
//  pID6.Value = "User";
SqlParameter pID9 = new SqlParameter("@Rate", SqlDbType.BigInt, 20);
pID9.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID10 = new SqlParameter("@Entry_Order_id", SqlDbType.BigInt, 20);
pID10.Direction = ParameterDirection.Output;
// pID6.Value = "User";




cmd.Parameters.Add(pID1);
cmd.Parameters.Add(pID2);
cmd.Parameters.Add(pID3);
cmd.Parameters.Add(pID4);
cmd.Parameters.Add(pID5);
cmd.Parameters.Add(pID6);
cmd.Parameters.Add(pID7);
cmd.Parameters.Add(pID8);
cmd.Parameters.Add(pID9);
cmd.Parameters.Add(pID10);

答案 1 :(得分:-1)

尝试删除

@Usd_Marg bigint OUTPUT,
@Amount bigint OUTPUT,
@Rate float OUTPUT,
@Entry_Order_id bigint OUTPUT

来自存储过程定义。

修改您的查询:

SELECT [Rate], [EntryOrder_ID],[Amount],[Usd_Mrg]  
FROM [dbo].[entrytable] 
WHERE [B_S]  = @B_S AND [Rate] = @RateE AND [Account_ID] = @Account_ID AND [Symbol] = @Symbol;

在C#端,您将收到一个数据集,因此您需要更改c#代码的最后一行:

cmd.ExecuteScalar();