SQL在表1中的表1中插入多行,其中包含来自Web表单C#的其他信息

时间:2015-06-18 07:44:10

标签: c# sql sql-server

我正在尝试从C#网络表单按下按钮,将表1中的数据插入到表2中。我只是将表1中的问题编号附加到表2,但我无法弄清楚如何添加从表单中添加的额外细节。 (DateStamp,用户)等

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();

1 个答案:

答案 0 :(得分:0)

这样的东西
INSERT INTO Table2 
(
 StressQuestionNumber, StressQuestionnaireID, CreatedBy, CreateDate
) 
 SELECT StressQuestionNumber, @StressQuestionnaireID, @CreatedBy, @CreateDate
   FROM Table1