First of all you really should be using using statements so your connections get closed in the event of an exception
using(SqlConnection con = new SqlConnection("**********************************************")) using(SqlCommand cmd = con.CreateCommand()) //The create command can happen before the open { con.Open(); cmd.CommandText = "INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date) VALUES ( @StudentID, @ISBN , @Title, @Date)"; //(Snip adding parameters) cmd.ExecuteNonQuery(); //You don't need to call close if you are using "using" }
That out of the way there are three ways to do it.
You could put both commands in a single command statement.
using(SqlConnection con = new SqlConnection("**********************************************")) using(SqlCommand cmd = con.CreateCommand()) { con.Open(); cmd.CommandText = @"INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date) VALUES ( @StudentID, @ISBN , @Title, @Date); INSERT INTO StudentActvitiy ([Student ID], Date) VALUES ( @StudentID, GETDATE())"; //(Snip adding parameters) cmd.ExecuteNonQuery(); }
or you could change the command text and run it again
using(SqlConnection con = new SqlConnection("**********************************************")) using(SqlCommand cmd = con.CreateCommand()) { con.Open(); cmd.CommandText = "INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date) VALUES ( @StudentID, @ISBN , @Title, @Date)"; //(Snip adding parameters) cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO StudentActvitiy ([Student ID], Date) VALUES ( @StudentID, GETDATE())" cmd.ExecuteNonQuery(); }
or you could do two commands
using(SqlConnection con = new SqlConnection("**********************************************")) using(SqlCommand cmd = con.CreateCommand()) using(SqlCommand cmd2 = con.CreateCommand()) { con.Open(); cmd.CommandText = "INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date) VALUES ( @StudentID, @ISBN , @Title, @Date)"; //(Snip adding parameters) cmd.ExecuteNonQuery(); cmd2.CommandText = "INSERT INTO StudentActvitiy ([Student ID], Date) VALUES ( @StudentID, GETDATE())" SqlParameter p21 = new SqlParameter("@StudentID", SqlDbType.NChar); p21.Value = textBox2.Text; cmd2.Parameters.Add(p21); cmd2.ExecuteNonQuery(); }
To do Tim's solution it is kind of a combination of the first and the 3rd.
using(SqlConnection con = new SqlConnection("**********************************************")) using(SqlCommand cmd = con.CreateCommand()) using(SqlCommand cmd2 = con.CreateCommand()) { con.Open(); cmd.CommandText = @"INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date) VALUES ( @StudentID, @ISBN , @Title, @Date); SELECT CAST(SCOPE_IDENTITY AS INT);"; //(Snip adding parameters) var resultId = (int)cmd.ExecuteScalar(); cmd2.CommandText = "INSERT INTO StudentActvitiy ([Student ID], Date, BorrowBookId) VALUES ( @StudentID, GETDATE(), @borrowBookId)" SqlParameter p21 = new SqlParameter("@StudentID", SqlDbType.NChar); p21.Value = textBox2.Text; cmd2.Parameters.Add(p21); SqlParameter p22 = new SqlParameter("@borrowBookId", SqlDbType.Int); p22.Value = resultId; cmd2.Parameters.Add(p22); cmd2.ExecuteNonQuery(); }
con.CreateCommand()twice.new SqlCommand("query", con);;SELECT CAST(SCOPE_IDENTITY AS INT);to the insert andint newID = cmd.ExecuteScalar();to execute the insert and retrieve it in one step.