Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1002081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:46:37+00:00 2026-05-16T07:46:37+00:00

this is my code I have: I have read that setting up the CommandText

  • 0

this is my code I have:

I have read that setting up the CommandText should happen only once and not in a loop… but how do I then get the individually item data from the foreach?

Someone smart enough to refactor that code, that would be nice 🙂

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

UPDATE: The solution that works!

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:46:38+00:00Added an answer on May 16, 2026 at 7:46 am

    Well, you could just move the loops:

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

    However, that seems a little ugly to me. I don’t know how the SQLite provider will behave, but I think it would be at least worth trying to create the SQLiteCommand as per your original code, adding the parameters but not their values… and then moving the ExecuteNonQuery code into the innermost foreach loop. Something like this:

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

    Just to reiterate, I don’t know if that will work – but I’d hope it would.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to read 100.000 rows from database, I use this code : List<elementClass>
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
this is function.php of a wordpress theme that NOD32 says this code have a
So i have this code which makes a box, but want to make the
I have read-only access to a database that was set up for a third-party,
I have read lots of questions and answers about this issue on StackOverflow, but
I have some code on my page that looks like this: <div class=img-center> <img
I have a data.frame from this code: my_df = data.frame(read_time = c(2010-02-15, 2010-02-15, 2010-02-16,
I've used this code to have a like btn on my site: <iframe src=http://www.facebook.com/plugins/like.php?href=URL_OF_YOUR_WEBSITE&amp;send=false&amp;layout=standard&amp;width=450&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font&amp;height=80&amp
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.