【问题标题】:using a while loop to insert multiple rows in C# and ado.net使用 while 循环在 C# 和 ado.net 中插入多行
【发布时间】:2013-05-12 03:54:48
【问题描述】:

我想为某个班级的老师分配班级: 我有一个变量,我希望所有变量都循环并在数据库中插入多行,如您所见,我输入了一个从 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 循环插入到许多行中?

【问题讨论】:

  • 定义一个包含你的值的类,将该类的一个实例添加到该类的列表中,遍历列表实例并为每个实例调用插入
  • 我是 c# 新手,并不完全了解其中的所有内容,我会尝试按照您提到的方式进行操作,但我不确定以正确的方式编写它。感谢史蒂夫的评论
  • 处理示例以显示伪代码以适应您的要求

标签: c#


【解决方案1】:

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

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

最后一步是循环遍历Lesson的List并调用insert到数据库,但首先用虚值创建insert中需要的所有参数,然后进入循环,分配真实值并执行查询

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

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

【讨论】:

  • 感谢史蒂夫,我将尝试使用您美丽的答案,我希望我能以正确的方式完成它..
  • 修复一个小错误(EndTime 不存在,它是下一个块的时间)
  • 我明白了,别担心兄弟。
  • 代码和我一起工作,循环工作,但它在第一个块上循环,第一个块插入了很多次,另一个没有插入,可能是我的错误或需要在代码上添加的东西.
  • 感谢您的耐心和支持
猜你喜欢
  • 1970-01-01
  • 2019-08-08
  • 2013-04-19
  • 2020-05-17
  • 2019-06-15
  • 1970-01-01
  • 2016-02-29
  • 2015-01-14
  • 2013-09-30
相关资源
最近更新 更多