使用while循环在C#和ado.net中插入多行

时间:2013-05-11 19:28:09

标签: c#

我想为某些班级的老师分配课程: 我有一个变量,我希望所有变量都循环并插入数据库中的许多行,因为你可以看到我把数字从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循环插入以插入许多行?

1 个答案:

答案 0 :(得分: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对象并在上一个列表中插入课程对象

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

最后一步是遍历List of Lesson并将插入调用到数据库,但首先使用虚拟值创建插入中所需的所有参数,然后进入循环,分配实际值并执行查询< / p>

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

请注意,这是伪代码,只是为了给你指示方向 您可以将其渲染为有效的代码。