Jump to content

Insert into two tables (at same time)


kvnmck18

Recommended Posts

I have a textbox and a datagrid that need to be inserted into two different tables.Textbox needs to go into table1 and the data grid needs to go into table2.

string sql = "insert into table1 (column1) values (blahblah.Text)";

The string sql is passed throughout the whole .cs, so is there a way to amend this string to include a second insert table?

Link to comment
Share on other sites

err...that's what I hoped not to hear. That makes this all so much more work to do.

Link to comment
Share on other sites

(the second string is called 'sql2')

comm.CommandText = sql, sql2;

How can I make something like this not be full of errors.What is the best way of doing 2 seperate tables at the same time?

Link to comment
Share on other sites

Why not try something like this:

string sql = "insert into table1 (column1) values (value1); insert into table2 (column1) values (value1);";

Link to comment
Share on other sites

doesn't work...I tried that already...
Well, then how about using stored procedures as I do.C#
SqlCommand command = new SqlCommand("MyAwesomeStoredProc", MySqlConnection);command.CommandType = CommandType.StoredProcedure;command.Parameters.AddWithValue("@Value1", value1);command.Parameters.AddWithValue("@Value2", value2);command.ExecuteNonQuery()

Stored Procedure

CREATE PROCEDURE MyAwesomeStoredProc(@Value1 int,@Value2 int)ASINSERT INTO Table1 (Column1) VALUES (@Value1)INSERT INTO Table2 (Column1) VALUES (@Value2)GO

Link to comment
Share on other sites

you could do this; (I'm using Sql server objects for the xample but oyu can change them how ever you like)

SqlConnection conn = new SqlConnection(....);SqlCommand cmd;string sql1 = "statement 1";string sql2 = "statement 2";conn.Open();cmd = new SqlCommand(sql1,conn);cmd.ExecuteNonQuery();cmd = new SqlCommand(sql2,conn);cmd.ExecuteNonQuery();conn.Close();

It should be that simple

Link to comment
Share on other sites

I wish that it was that simple, the first original string is processed so much...so adding it in just one process doesn't work. Look at the code again. The "if (!(recordexist(sql)))" part.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...