Pepsi
Pepsi

Reputation: 19

How to use SqlCommand with included two queries

First question, I would like to implement two queries in one command.

What is the correct syntax that should I followed?

I tried this

SqlCommand cmd = new SqlCommand("BEGIN;" +
                " INSERT INTO Booking(Arrive, leave, RoomId)" +
                " VALUES(@Arrive, @leave, @RoomId);" +
                " INSERT INTO Customer(CustomerId, CustomerName, birth_day, City, Phone, Email, Gender)" +
                " VALUES(@CustomerId, @CustomerName, @birthday, @City, @Phone, @Email, @Gender); COMMIT; ", con)

Second one : can I put "if statement" before SqlCommand ?

For example :

if (x = 1) 
{
    SqlCommand cmd = new SqlCommand("BEGIN;" +
                " INSERT INTO Booking(Arrive, leave, RoomId)" +
                " VALUES(@Arrive, @leave, @RoomId);" +
                " INSERT INTO Customer(CustomerId, CustomerName, birth_day, City, Phone, Email,Gender)" +
                " VALUES(@CustomerId, @CustomerName, @birthday, @City, @Phone, @Email, @Gender);COMMIT; ", con)
}
else
{ 
    SqlCommand cmd = new SqlCommand("insert into Customer(CustomerId, CustomerName, birth_day, City, Phone, Email, Gender)" +
                " values(@CustomerId, @CustomerName, @birthday, @City, @Phone, @Email, @Gender)", con)
}

Upvotes: 0

Views: 59

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

Yes, you can use stored procedure and call as many as query inside it.

Yes you can use if statement but it should be if(x == 1.

I think your query should also work, don't need begin and commit

Upvotes: 1

Related Questions