0

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)
}

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I think my mistake in first query is semicolon, Should I remove all ";" ? , If yes, should I replace them by something else like "+" ?
@Pepsi use this ` " 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);`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.