如何在ASP.NET中使用输出参数插入存储过程

时间:2018-12-28 18:24:50

标签: c# asp.net stored-procedures sql-server-2012

我的存储过程如下

ALTER PROCEDURE [dbo].[sp1]
    @mat NVARCHAR(1000) = NULL,
    @party NVARCHAR(1000) = NULL,
    @place NVARCHAR(1000) = NULL,
    @truk NVARCHAR(1000) = NULL,
    @qty NUMERIC(10) = NULL,
    @ptm NUMERIC(10) = NULL,
    @mop NVARCHAR(100) = NULL,
    @tos NVARCHAR(100) = NULL,
    @driver NVARCHAR(100) = NULL,
    @date1 DATE = NULL
AS
BEGIN
    DECLARE @sql NVARCHAR(4000); 
    DECLARE @params NVARCHAR(4000); 
    DECLARE @rate NUMERIC(10); 

    SET @sql ='select  @rate = ['+@mat+']   from tblcos'+ ' where [Name] = @party' 
    set @params = '@party nvarchar (1000), @rate NVARCHAR(10) OUTPUT'  
    exec sp_executesql @sql, @params,@party= @party,@rate = @rate OUTPUT

    INSERT INTO tblsls([Party], [Place], [truk], [Material], [Qty], rate, [Amount], [Payment], [Balance], [MOP], [TOS], [driver], [Date]) 
        SELECT 
            @party, @place, @truk, @mat, @rate, @qty, (@qty * @rate),
            @ptm, (@qty * @rate - @ptm), @mop, @tos, @driver, @date1
END

我的aspx.cs文件

 protected void Button1_Click(object sender, EventArgs e)
 {
     SqlConnection con = new SqlConnection (@"
     Data Source = ABC - PC; Initial Catalog = VRA; Integrated Security = True");

     SqlCommand cmd = new SqlCommand("sp1", con);
     cmd.CommandType = CommandType.StoredProcedure;

     cmd.Parameters.AddWithValue("Date", Calendar1.SelectedDate);
     cmd.Parameters.AddWithValue("party", Ddprt.SelectedValue);
     cmd.Parameters.AddWithValue("Place", tbpls.Text);
     cmd.Parameters.AddWithValue("Truk", Tbtru.Text);
     cmd.Parameters.AddWithValue("Material", Ddmat.SelectedValue);
     cmd.Parameters.AddWithValue("Qty", Tbqty.Text);
     **cmd.Parameters.AddWithValue("Rate",         );**
     **cmd.Parameters.AddWithValue("Amount",          );**
     cmd.Parameters.AddWithValue("Payment", Tbptm.Text);
     **cmd.Parameters.AddWithValue("Balance",            );**
     cmd.Parameters.AddWithValue("MOP", Ddmop.SelectedValue);
     cmd.Parameters.AddWithValue("TOS", Ddtos.SelectedValue);
     cmd.Parameters.AddWithValue("driver", Tbdri.Text);

     con.Open();

     int k = cmd.ExecuteNonQuery();

     if (k != 0)
     {
         Lbmsg.Text = "Record inserted successfully into the database";
         Lbmsg.ForeColor = System.Drawing.Color.CornflowerBlue;
     }

     con.Close();
}

如何使RateAmountBalance从存储过程sp1中获取值并更新值?

1 个答案:

答案 0 :(得分:1)

创建另一个接受RateAmountBalance作为参数的过程,然后从您的Web应用程序中调用该过程。这样,您就不会破坏现有sp1的功能,但仍然拥有所需的东西。

我对sql的语法了解不多,但我认为它看起来像这样。如果有错,请有人纠正。

ALTER PROCEDURE [dbo].[sp1_fromWeb]
    @mat NVARCHAR(1000) = NULL,
    @party NVARCHAR(1000) = NULL,
    @place nvarchar(1000)=null,
    @truk nvarchar (1000)=null,
    @qty numeric (10)=null,
    @ptm numeric(10)=null,
    @mop nvarchar(100)=null,
    @tos nvarchar(100)=null,
    @driver nvarchar(100)=null,
    @date1 date = null,
    @rate numeric(10),
    @amount numeric(10),
    @balance numeric(10)
AS
BEGIN
    DECLARE @sql NVARCHAR(4000); 
    DECLARE @params NVARCHAR(4000); 

    SET @sql ='select  @rate = ['+@mat+']   from tblcos'+ ' where [Name] = @party' 
    set @params = '@party nvarchar (1000), @rate NVARCHAR(10) OUTPUT'  
    exec sp_executesql @sql, @params,@party= @party,@rate = @rate OUTPUT

    INSERT INTO tblsls([Party],[Place],[truk], [Material],[Qty], rate,[Amount],[Payment],[Balance],[MOP],[TOS],[driver],[Date]) 
        SELECT @party, @place, @truk, @mat, @qty, @rate, @amount, @ptm, @balance, @mop, @tos, @driver, @date1
END