简体   繁体   中英

SQL Insert into multiple rows in Table 1 from Table 2 with additional information from web form C#

I'm trying to insert data from Table1 to Table2 on a button press from a C# web form. I've got it working with appending just the question numbers from Table1 to Table2 but I can't figure out how to add extra details to be added from the form. (DateStamp, user) etc

SqlConnection conn10000 = new SqlConnection(ConfigurationManager.ConnectionStrings["ESRConnectionString"].ConnectionString);
conn10000.Open();
string AppendQuestions = "INSERT INTO Table2 " +
                         " (StressQuestionNumber, StressQuestionnaireID, CreatedBy, CreateDate) VALUES( " +
                         "SELECT StressQuestionNumber " +
                         "FROM Table1, " +
                         "@StressQuestionnaireID, @CreatedBy, @CreateDate )";
SqlCommand com10000 = new SqlCommand(AppendQuestions, conn10000);
com10000.Parameters.AddWithValue("@StressQuestionnaireID", StressID);
com10000.Parameters.AddWithValue("@CreatedBy", Session["sesUserLogIn"]);
com10000.Parameters.AddWithValue("@CreateDate", DateTime.Now);
com10000.ExecuteScalar();
conn10000.Close();

Something like

INSERT INTO Table2 
(
 StressQuestionNumber, StressQuestionnaireID, CreatedBy, CreateDate
) 
 SELECT StressQuestionNumber, @StressQuestionnaireID, @CreatedBy, @CreateDate
   FROM Table1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM