【问题标题】:asp SQL Statement to insert multiple rows of record into Tableasp SQL 语句将多行记录插入表中
【发布时间】:2014-02-05 18:29:03
【问题描述】:

我正在尝试创建一个出勤表来记录学生参与的活动。创建新活动时会触发以下代码。创建新活动时,我想将所有学生记录插入到考勤表中,并将默认参加属性标记为 false。 (attend属性用来标记出勤,设为0)

问题是考勤表无法填充。代码将在以下行停止:numofRecordsAffected = command.ExecuteNonQuery();

我正在尝试从学生表中获取所有 sigstudentid(s)。我不知道在这种情况下是否建议使用 foreach(),但我们将不胜感激。

我需要一个解决方案!

    public int InsertAttendance(int num)
        {
        SqlDataAdapter adapter = new SqlDataAdapter();
        SqlCommand command = new SqlCommand();
        SqlConnection connect = new SqlConnection();
        DataSet dataset = new DataSet();
        String sqlText = "SELECT * FROM Student";
        connect.ConnectionString = "Data Source=TECKISTTLE; Initial Catalog=Assignment;    Integrated Security=True";
        command.Connection = connect;
        command.CommandText = sqlText;

        int numofRecordsAffected = 0;

        adapter.SelectCommand = command;
        connect.Open();
        adapter.Fill(dataset, "StudentData");
        sqlText = "";

        foreach (DataRow item in dataset.Tables["StudentData"].Rows)
        {
            sqlText += " INSERT INTO Attendance(SIGStudentID, ActivityId, Attendance) VALUES(@SIGStudentID,@ActivityId,@Attendance); ";
            command.Parameters.Clear();
            command.Parameters.Add("@SIGStudentID", SqlDbType.Int);
            command.Parameters["@SIGStudentID"].Value = Convert.ToInt32(item["SIGStudentID"]);
            command.Parameters.Add("@ActivityId", SqlDbType.Int);
            command.Parameters["@ActivityId"].Value = num;
            command.Parameters.Add("@Attendance", SqlDbType.Bit);
            command.Parameters["@Attendance"].Value = 0;



        }
        numofRecordsAffected = command.ExecuteNonQuery();

        connect.Close();

        return numofRecordsAffected;
    }

【问题讨论】:

  • 您使用的是哪个版本的 SQL Server??
  • 我正在使用 Microsoft Sql Server Management Studio 2012
  • 那是 不是 ASP Classic(这将是 VBA 代码而已)- 我假设您的意思是 asp.net 而不是

标签: asp.net sql sql-server insert rows


【解决方案1】:

当您将代码 numofRecordsAffected = command.ExecuteNonQuery(); 放在您的 foreach 之外时。 它只执行一次。由于这段代码是对表中INSERT数据的实际语句,所以它将INSERT只有一个ROW最后一个。

试着输入你的代码

numofRecordsAffected = command.ExecuteNonQuery();

foreach 块内。然后它将INSERT每一行。

    public int InsertAttendance(int num)
    {
    SqlDataAdapter adapter = new SqlDataAdapter();
    SqlCommand command = new SqlCommand();
    SqlConnection connect = new SqlConnection();
    DataSet dataset = new DataSet();
    String sqlText = "SELECT * FROM Student";
    connect.ConnectionString = "Data Source=TECKISTTLE; Initial Catalog=Assignment;    Integrated Security=True";
    command.Connection = connect;
    command.CommandText = sqlText;

    int numofRecordsAffected = 0;

    adapter.SelectCommand = command;
    connect.Open();
    adapter.Fill(dataset, "StudentData");
    sqlText = "";
    numofRecordsAffected = 0;
    sqlText = "INSERT INTO Attendance(SIGStudentID, ActivityId, Attendance) VALUES(@SIGStudentID,@ActivityId,@Attendance); ";
    command.CommandText = sqlText;
    foreach (DataRow item in dataset.Tables["StudentData"].Rows)
    {
        command.Parameters.Clear();
        command.Parameters.AddWithValue("@SIGStudentID", Convert.ToInt32(item["SIGStudentID"]);
        command.Parameters.AddWithValue("@ActivityId", num);
        command.Parameters.AddWithValue("@Attendance", 0);

        numofRecordsAffected += command.ExecuteNonQuery();

    }


    connect.Close();

    return numofRecordsAffected;
}

【讨论】:

  • 考勤表无法接收来自上述代码的任何记录。有什么需要注意的吗?
  • 我已经在我的回答中修改了你的代码,看看它是否有效。
  • 谢谢,接受我的回答并投票表示感谢。
  • 需要 15 声望。我会建立起来
  • 好的,但由于这是您的问题,即使您没有声誉,您也可以接受答案。
【解决方案2】:

您可能通过使用循环将其复杂化了一点,而可以这样做:

"INSERT INTO Attendance(SIGStudentID, ActivityId, Attendance) select SIGStudentID, " + num + " as ActivityId, 0 as Attendance from Student;"

参考:

insert one column data from a table into other table, but the other column data will be specified dynamically

Insert all values of a table into another table in SQL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多