数据插入使用输入输出存储过程

时间:2013-11-18 15:52:34

标签: c# asp.net sql ado.net

我正在使用ASP.net C#创建一个Web应用程序。我有一个预订表格,我需要使用存储过程将数据插入表格。该表有几列,其中第二列是计算列。存储过程设置为插入数据并在插入后从第二列获取值。以下是存储过程的代码:

    Create Procedure sp_InsertCashPooja
@FirstName varchar(100),
@LastName varchar(100),
@TelNo bigint,
@Star char(50),
@Rasi char(50),
@Gothram char(50),
@PDMID int,
@PayMode bit,
@PujaName char(50),
@DonateAmt decimal(19,2),
@RcptNo varchar(25) output

as

Begin

SET NOCOUNT ON;

BEGIN TRY

BEGIN TRANSACTION

    if @PujaName != 'DONATION'
    Begin

        INSERT INTO PoojaDetails (FirstName, LastName, TelNo, Star, Rasi, Gothram, PoojaDietyMasterID, PayMode) values (@FirstName,@LastName,@TelNo,@Star,@Rasi,@Gothram,@PDMID,@PayMode)

    End

    if @PujaName = 'DONATION'
    Begin

        DECLARE @isDonate int = 0;

        INSERT INTO PoojaDetails (FirstName, LastName, TelNo, Star, Rasi, Gothram, PoojaDietyMasterID, PayMode, isDonate, DonateAmount) values (@FirstName,@LastName,@TelNo,@Star,@Rasi,@Gothram,@PDMID,@PayMode, @isDonate, @DonateAmt)

    End

    Select @RcptNo = max(ReceiptNo) from PoojaDetails
    Return @RcptNo
COMMIT TRANSACTION

END TRY

BEGIN CATCH
    IF (@@TRANCOUNT > 0)

    ROLLBACK TRANSACTION

END CATCH

SET NOCOUNT OFF;


End

我想在点击按钮时插入数据:我能够找出以下代码......

 protected void btnSave_Click(object sender, EventArgs e)
        {


            frmFirstName = txtFirstName.Text.Trim().ToUpper();
            frmLastName = txtLastName.Text.Trim().ToUpper();
            frmPhoneNo = Convert.ToInt32(txtPhoneNo.Text.Trim());
            frmNakshatra = Convert.ToString(cmbNakshatra.SelectedItem).Trim();
            frmRasi = Convert.ToString(cmbRasi.SelectedItem).Trim();
            frmGothram = Convert.ToString(cmbGothram.SelectedItem).Trim();
            frmPujaName = Convert.ToString(cmbPujaName.SelectedItem).Trim();
using (SqlConnection connection = new SqlConnection())
        {
            if (frmPayMode == "Cash")
            {
                if (frmPujaName == "DONATION")
                {
                    SqlDataAdapter CashAdapter = new SqlDataAdapter();

                    CashAdapter.InsertCommand = new SqlCommand("sp_InsertCashPooja", connection);
                    CashAdapter.InsertCommand.CommandType = CommandType.StoredProcedure;

请帮助....我想捕获返回的RcptNo,然后打算调用另一个ASPX页面并使用查询字符串传递该值。

由于

3 个答案:

答案 0 :(得分:1)

使用简单的SqlCommand来调用您的SP

connection.Open();
var cmd = new SqlCommand("sp_InsertCashPooja", connection);
cmd.Parameters.AddWithValue("FirstName", frmFirstName);
// Add all the others parameters in same way
var id = (int)cmd.ExecuteScalar();
connection.Close();

答案 1 :(得分:1)

将返回变量更改为:

Select @RcptNo = SCOPE_IDENTITY()

它将返回在此过程中为插入记录创建的标识号。

答案 2 :(得分:1)

使用sql参数..

connection = ConfigurationManager.AppSettings [“mycon”];

        SqlParameter[] para = new SqlParameter[2];
        para[0] = new SqlParameter("@stored procedure column name", string name);
        para[1] = new SqlParameter("@stored procedure column name", string name);