chalupabatman
Member
- Joined
- Nov 6, 2014
- Messages
- 8
- Programming Experience
- Beginner
I am in the beginning stages of writing my syntax for two insert statements in one connection (this doesn't throw any errors in the compiler, but it may crash since I haven't executed it yet). I was going to ask, before I start teaching myself bad principles/practices, is this the best way to perform 2 insert statements in one connection?
C#:
string cmd1 = "INSERT INTO tableInfo (ItemNumber,soldby) VALUES (@ItemNumber, @soldby)";
string cmd2 = "INSERT INTO saleInfo (ProgramID,StoreNumber) VALUES (@ItemNumber, @StoreNumber)";
using (SqlConnection conn = new SqlConnection(connString.SqlServerConnString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = cmd1;
comm.Parameters.AddWithValue("@ItemNumber", textbox_StoreName.Text);
comm.Parameters.AddWithValue("@soldby", txtsoldby.Text);
try
{
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
catch (SqlException e) { MessageBox.Show(e.ToString()); }
}
using (SqlCommand comm1 = new SqlCommand())
{
comm1.Connection = conn;
comm1.CommandText = cmd2;
comm1.Parameters.AddWithValue("@ItemNumber", textbox_StoreName.Text);
comm1.Parameters.AddWithValue("@StoreNumber", "18643");
comm1.Parameters.AddWithValue("@ItemNumber", textbox_StoreName.Text);
comm1.Parameters.AddWithValue("@StoreNumber", "55241");
comm1.Parameters.AddWithValue("@ItemNumber", textbox_StoreName.Text);
comm1.Parameters.AddWithValue("@StoreNumber", "818737);
comm1.Parameters.AddWithValue("@ItemNumber", textbox_StoreName.Text);
comm1.Parameters.AddWithValue("@StoreNumber", "152423");
comm1.Parameters.AddWithValue("@ItemNumber", textbox_StoreName.Text);
comm1.Parameters.AddWithValue("@StoreNumber", "991177");
try
{
conn.Open();
comm1.ExecuteNonQuery();
conn.Close();
}
catch (SqlException e) { MessageBox.Show(e.ToString()); }
}
}