简体   繁体   中英

How do I execute a Stored Procedure in C#?

Using the following SQL Stored Procedure, how would I execute it within the event handler of the btnYes Button? I don't know how to execute Stored Procedures. I have a TextBox set up to input the RoomNo . When I click btnYes , the expected outcome should be that the Occupied boolean is set to true in the tblRooms table based on it's RoomNo .

CREATE PROCEDURE sproc_tblRooms_Update
    -- Parameters for the UPDATE statement
    @RoomNo   INT,
    @Occupied BIT
AS
    UPDATE tblRooms
    SET Occupied = @Occupied
    WHERE RoomNo = @RoomNo

Update() method set up in a class:

public void Update(clsRoom RoomOccupancy)
{
    // This function will update an existing reservation in the database

    // Connect to the database
    clsDataConnection DBRoomVacancy = new clsDataConnection();

    // Add the parameters
    DBRoomVacancy.AddParameter("@RoomNo", RoomOccupancy.RoomNo);
    DBRoomVacancy.AddParameter("@Occupied", RoomOccupancy.Occupied);

    // Execute the query
    DBRoomVacancy.Execute("sproc_tblRooms_Update");
}

Event handler for the btnYes button that I want to click to execute the Stored Procedure:

protected void btnYes_Click(object sender, EventArgs e)
{

}

You should be able to call the Update() method after creating an instance of the class that the Update() method is a member of. Since you didn't provide the full class definition, I'm just going to call it MyClass in the example below:

protected void btnYes_Click(object sender, EventArgs e)
{
    var myClass = new MyClass();
    myClass.Update(new clsRoom
    {
        RoomNo = 1234,
        Occupied = true
    });
}

That being said, as an off-topic note, your clsDataConnection class needs a lot of work. I didn't look at it long, but what jumped out at me is that none of the classes that implement IDisposable (like SqlConnection , SqlCommand , etc) are wrapped in a using block or call Dispose() .

I also notice that the class uses a shared instance of SqlConnection for all of it's queries. Best practice is to create one instance of SqlConnection per query .

I would also invert some of your if statements and use return to get rid of some of your unnecessary else blocks. The reason for this is to reduce unnecessary nesting and avoid the Pyramid of Doom / Arrow code .

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class news : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 

    }
    protected void btnYes_Click(object sender, EventArgs e)
    {
        try
        {

            string str = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            SqlConnection con = new SqlConnection(str );
            SqlCommand cmd = new SqlCommand("sproc_tblRooms_Update", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@RoomNo", SqlDbType.VarChar).Value = txttitle.Text);
            cmd.Parameters.Add("@Occupied ", SqlDbType.VarChar).Value = Occupied.SelectedValue.ToString();
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Text = "Room has been Updated Successfully";
        }
        catch (Exception)
        { }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM