0

我想为某个班级的老师分配班级:我有一个变量,我希望所有变量都循环并在数据库中插入许多行,正如你所看到的,我输入了一个从 1 到 8 的数字我希望每组数字都是以多行插入数据库,而不是仅插入数字 1 。

int count = 0;
string classid = DropDownList1.SelectedValue.ToString();
string period_1 = period1.Text;
string time_1 = time1.Text;
string weekday1 = "1";
string course_1 = Scourses1.SelectedValue.ToString();
string teach_1 = Steacher1.SelectedValue.ToString();
string time_2 = time2.Text;
string weekday2 = "2";
string course_2 = Scourses2.SelectedValue.ToString();
string teach_2 = Steacher2.SelectedValue.ToString();
string time_3 = time3.Text;
string weekday3 = "3";
string course_3 = Scourses3.SelectedValue.ToString();
string teach_3 = Steacher3.SelectedValue.ToString();
string time_4 = time4.Text;
string weekday4 = "4";
string course_4 = Scourses4.SelectedValue.ToString();
string teach_4 = Steacher4.SelectedValue.ToString();
string time_5 = time5.Text;
// string weekday5 = weekd5.Text;
string course_5 = Scourses5.SelectedValue.ToString();
string teach_5 = Steacher5.SelectedValue.ToString();
string period_2 = period2.Text;
string time_6 = time6.Text;
//string weekday6 = weekd1.Text;
string course_6 = Scourses6.SelectedValue.ToString();
string teach_6 = Steacher6.SelectedValue.ToString();
string time_7 = time7.Text;
//string weekday7 = weekd2.Text;
string course_7 = Scourses7.SelectedValue.ToString();
string teach_7 = Steacher7.SelectedValue.ToString();
string time_8 = time8.Text;
// string weekday8 = weekd3.Text;
string course_8 = Scourses8.SelectedValue.ToString();
string teach_8 = Steacher8.SelectedValue.ToString();
string time_9 = time9.Text;
string weekday9 = weekd4.Text;
string course_9 = Scourses9.SelectedValue.ToString();
string teach_9 = Steacher9.SelectedValue.ToString();
string time_10 = time10.Text;
string weekday10 = weekd5.Text;
string course_10 = Scourses10.SelectedValue.ToString();
string teach_10 = Steacher10.SelectedValue.ToString();

//and the query to insert into database is :
string query = "INSERT INTO dbo.teacher_classes (period, weekday, course, teach_id, time, class_id) " +
               "VALUES (@Period, @Weekday, @Course, @Teach_id, @Time, @Class) ";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Period", period_1);
cmd.Parameters.AddWithValue("@Weekday", weekday1);
cmd.Parameters.AddWithValue("@Course", course_1);
cmd.Parameters.AddWithValue("@Teach_id", teach_1);
cmd.Parameters.AddWithValue("@Time", time_1);
cmd.Parameters.AddWithValue("@Class", classid);
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
count = cmd.ExecuteNonQuery();

con.Close();

//the only variables inserted is:
string classid = DropDownList1.SelectedValue.ToString();
string period_1 = period1.Text;
string time_1 = time1.Text;
string weekday1 = "1";
string course_1 = Scourses1.SelectedValue.ToString();
string teach_1 = Steacher1.SelectedValue.ToString();

我想将它们作为一个while循环插入到多行中,那是?

4

1 回答 1

3

首先定义一个类来包含你的值(我们使用的是面向对象的语言,对吧?)

public class Lesson
{
   pulic int LessonID {get; set;}          
   public string Period {get; set;}          
   public string  StartTime  {get; set;}          
   public string weekday {get; set;}          
   public int CourseID  {get; set;}          
   public int TeacherID  {get; set;}          
}

现在在您的代码中定义一个 List(of MyLesson),您可以在其中添加课程

public List<Lesson> myLessons = new List<Lesson>();

下一步是从您的控件中提取值,添加到课程对象并将课程对象插入上一个列表中

Lesson aLesson = new Lesson();
aLesson.LessonID = Convert.ToInt32(DropDownList1.SelectedValue);
aLesson.Period = period1.Text;
aLesson.StartTime = time1.Text;
aLesson.Weekday = "1";
aLesson.CourseID = Convert.ToInt32(Scourses1.SelectedValue);
aLesson.TeacherID = Convert.ToInt32(Steacher1.SelectedValue);
myLessons.Add(aLesson); 
// Repeat for the next block of fields

最后一步是循环遍历课程列表并调用插入到数据库中,但首先使用虚拟值创建插入中所需的所有参数,然后进入循环,分配真实值并执行查询

string query = "INSERT INTO dbo.teacher_classes (period, weekday, course, teach_id, " + 
               "time, class_id) VALUES (@Period, @Weekday, @Course, @Teach_id, @Time, @Class) ";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Period", "");
cmd.Parameters.AddWithValue("@Weekday", "");
cmd.Parameters.AddWithValue("@Course", 0);
cmd.Parameters.AddWithValue("@Teach_id", 0);
cmd.Parameters.AddWithValue("@Time", "");
cmd.Parameters.AddWithValue("@Class", 0);
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;

foreach(Lesson aLesson in myLessons)
{
   cmd["@Period"].Value = aLesson.Period;
   cmd["@Weekday"].Value = aLesson.Weekday;
   cmd["@Course"].Value = aLesson.CourseID;
   cmd["@Teach_id"].Value = aLesson.TeacherID;
   cmd["@Time"].Value = aLesson.StartTime;
   cmd["@Class"].Value = aLesson.ClassID;
   count = cmd.ExecuteNonQuery();
}

请注意,这是伪代码,只是为了给你遵循的方向。
您可以将其呈现为工作代码。

于 2013-05-11T19:48:51.983 回答