我试图创建一个考勤表来记录学生参与的活动。下面的代码在创建新活动时触发。创建新活动时,我想将所有学生记录插入考勤表并将默认的考勤属性标记为false。 (出席属性为标记出勤,设为0)asp SQL语句将多行记录插入到表中
问题是出席表不能填充。代码将停止在该行继续: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服务器管理工作室2012 –
这是**不** ** ASP经典(这将是VBA代码只) - 我假设你的意思是'asp.net'而不是 –