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 8520195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:28:03+00:00 2026-06-11T06:28:03+00:00

I have a method where I insert some data in a local MDB-Acces file.

  • 0

I have a method where I insert some data in a local MDB-Acces file. With this code it works fine:

try
{
   foreach (DataRow rowS99 in dtS99.Rows)
   {
      con.Open();
      da.InsertCommand = con.CreateCommand();
      da.InsertCommand.Parameters.AddWithValue("@wertListName", rowS99["WertListName"]);
      da.InsertCommand.Parameters.AddWithValue("@key", rowS99["Key"]);
      da.InsertCommand.Parameters.AddWithValue("@bezeichner", rowS99["Bezeichner"]);
      da.InsertCommand.Parameters.AddWithValue("@keyAufbereitet", rowS99["KeyAufbereitet"]);
      da.InsertCommand.CommandText = sql;
      da.InsertCommand.ExecuteNonQuery();
      con.Close();
   }
   check = true;
}

When i put the Open and Close method outside the foreach loop the code works, but the local file, which was used with the connection, is already opened with Visual Studio so that i can’t delete or move it. The Status of the connection says after con.Close() that it is closed.

What is the problem here?

  • 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-06-11T06:28:05+00:00Added an answer on June 11, 2026 at 6:28 am

    You don’t need to have the Connection Opened and Closed within the loop. If there are 500 rows in your table then you will be opening & closing the connection 500 times, its not best practice..

    Your connection should open in the Try statement before the loop and close once all Rows have been processed.

    I think you would be best using the Update() function rather than the method your doing above.

    You can take out the need to loop through each and every row.

    http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx

    Using this functionality allows you to add, edit and delete rows simply by calling the Update() method. You will need all 3 Commands setting up, UPDATE, INSERT, DELETE.

    Hope this helps

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

Sidebar

Related Questions

I have a method to insert data into server like this: public void doInsert(){
This is the insert method I am using to insert some data into the
I have a method that should post some data to a PHP file: -(void)submitForm
Hi I have this method below which should insert values into my database. However
My program have ability to export some data and DataTable to Excel file (template)
I want to insert some data to mydatabase.sqlite but I have a problem. There
I have an OperationContract method where I am trying to query and insert data
I have a method which does a simple mysql insert, when I tried to
i have an abstract class BaseClass with a public insert() method: public abstract class
I am trying to learn LINQ to SQL. I have successfully implemented insert method

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.