在C#中的几个foreach循环中执行SQLite插入?

时间:2010-08-21 20:57:00

标签: c# sqlite insert foreach

这是我的代码:

我已经读过,设置CommandText应该只发生一次而不是循环...但是如何从foreach中获取单独的项目数据?

有人足够聪明地重构代码,那会很好:)

using (SQLiteTransaction trans = DataAccess.ConnectionManager.BeginTransaction())
{  
    using (SQLiteCommand com = new SQLiteCommand(DataAccess.ConnectionManager))
    {
        // How can I add the parameters here if they are only known in the foreach loop?
        com.Parameters.Add(new SQLiteParameter("@date", day.SchooldayDate));
        com.Parameters.Add(new SQLiteParameter("@periodnumber", period.PeriodNumber));
        com.Parameters.Add(new SQLiteParameter("@schoolclasscode", period.SchoolclassCode));


        foreach (var week in weekList)
        {
            foreach (var day in week.Days)
            { 
                foreach (var period in day.Periods)
                {
                    com.CommandText = "Insert into tablename (date,periodnumber,schoolclasscode) Values (@date,@periodnumber,@schoolclasscode)";

                }
            }
        }

        com.ExecuteNonQuery();
    }
    trans.Commit();                
}

更新:有效的解决方案!

using (SQLiteTransaction trans = DataAccess.ConnectionManager.BeginTransaction())
            {
                using (SQLiteCommand com = new SQLiteCommand(DataAccess.ConnectionManager))
                {
                    com.CommandText = "Insert into lessonday (lessondate,lessonnumber,schoolclasscode) VALUES (@lessondate,@lessonnumber,@schoolclasscode)";

                    SQLiteParameter p1 = new SQLiteParameter("@lessondate", DbType.DateTime);
                    SQLiteParameter p2 = new SQLiteParameter("@lessonnumber", DbType.Int32);
                    SQLiteParameter p3 = new SQLiteParameter("@schoolclasscode", DbType.String);

                    com.Parameters.Add(p1);
                    com.Parameters.Add(p2);
                    com.Parameters.Add(p3);

                    foreach (var week in weekList)
                    {
                        foreach (var day in week.Days)
                        {
                            p1.Value = day.SchooldayDate;
                            foreach (var period in day.Periods)
                            {
                                p2.Value = period.PeriodNumber;
                                p3.Value = period.SchooclassCode;

                                com.ExecuteNonQuery();
                            }
                        }
                    }
                }
                trans.Commit();
            }
        }

1 个答案:

答案 0 :(得分:0)

好吧,你可以移动循环:

using (SQLiteTransaction trans = DataAccess.ConnectionManager.BeginTransaction()) 
{  
  foreach (var week in weekList)
  {
    foreach (var day in week.Days)
    { 
      foreach (var period in day.Periods)
      {
        using (SQLiteCommand com = new SQLiteCommand(...))
        {   
          com.Parameters.Add(new SQLiteParameter(...));
          com.Parameters.Add(new SQLiteParameter(...));
          com.Parameters.Add(new SQLiteParameter(...));
          com.CommandText = "Insert into ...";
          com.ExecuteNonQuery();
        }
      }
    }
  }
  trans.Commit();                
} 

然而,这对我来说似乎有点难看。我不知道SQLite提供程序将如何表现,但我认为至少值得尝试根据您的原始代码创建SQLiteCommand,添加参数但而不是他们的价值 ...然后将ExecuteNonQuery代码移动到最里面的foreach循环中。像这样:

using (SQLiteTransaction trans = DataAccess.ConnectionManager.BeginTransaction())
{  
  using (SQLiteCommand com = new SQLiteCommand(DataAccess.ConnectionManager))
  {   
    com.CommandText = "Insert into ...";
    SQLiteParameter p1 = com.Parameters.Add(new SQLiteParameter(...));
    SQLiteParameter p2 = com.Parameters.Add(new SQLiteParameter(...));
    SQLiteParameter p3 = com.Parameters.Add(new SQLiteParameter(...));
    foreach (var week in weekList)
    {
      p1.Value = week;
      foreach (var day in week.Days)
      { 
        p2.Value = day;
        foreach (var period in day.Periods)
        {
          p3.Value = period;               
          com.ExecuteNonQuery();
        }
      }
    }
  }
  trans.Commit();                
}

重申一下,我不知道这是否有效 - 但我希望希望