Question: I have a task to add as many users to Access database as user wants to. (inputs a number of persons to add).

Now, my code only adds first one that I wrote. How do I need to change the code so that it would add next one, and if I input 10 persons that it would add all of them?

```
 string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"dbs.accdb\"";
        OleDbConnection con = new
       OleDbConnection(ConnectionString);
        OleDbCommand addCmd = new OleDbCommand("INSERT INTO PERSON(Name, LastName) VALUES(?,?)", con);
        Console.Write("A number of persons to add: ");
        int number= Convert.ToInt32(Console.ReadLine());
        for (int i = 0; i < number; i++)
        {
            Console.Write("Name:");
            string name= Console.ReadLine();
            addCmd.Parameters.Add(new OleDbParameter("@Name", name));
            Console.Write("Last Name:");
            string lastName= Console.ReadLine();
            addCmd.Parameters.Add(new OleDbParameter("@LastName", lastName));

        }
        con.Open();
        addCmd.ExecuteNonQuery();
        con.Close();

```

Answer: 
Just move the command and its execution inside the loop while the connection should be created and opened outside the loop

```
string cmdText = "INSERT INTO PERSON(Name, LastName) VALUES(?,?)";
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"dbs.accdb\"";
using(OleDbConnection con = new OleDbConnection(ConnectionString))
using(OleDbCommand addCmd = new OleDbCommand(cmdText, con))
{
    con.Open();
    addCmd.Parameters.Add("@Name", OleDbType.VarWChar);
    addCmd.Parameters.Add("@LastName", OleDbType.VarWChar);

    Console.Write("A number of persons to add: ");
    int number= Convert.ToInt32(Console.ReadLine());
    for (int i = 0; i < number; i++)
    {
        {
           Console.Write("Name:");
           string name= Console.ReadLine();
           addCmd.Parameters["@Name"].Value = name;
           Console.Write("Last Name:");
           string lastName= Console.ReadLine();
           addCmd.Parameters["@LastName"].Value = lastName;
           addCmd.ExecuteNonQuery();
       }
   }
}

```

Notice that all disposable objects (the connection and the command) are enclosed in a using statement to get proper disposal when you have done with it (and for the connection this means also that you don't need to call Close)
Also as pointed in comments below, you can create the parameters outside the loop and then change their value inside the loop (and thus avoiding the creation of the command and the parameters for each loop)
```
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"dbs.accdb\"";
        OleDbConnection con = new OleDbConnection(ConnectionString);

        Console.Write("A number of persons to add: ");
        int number= Convert.ToInt32(Console.ReadLine());
        for (int i = 0; i < number; i++)
        {
            OleDbCommand addCmd = new OleDbCommand("INSERT INTO PERSONA(Name, LastName) VALUES(?,?)", con);

            con.Open();
            Console.Write("Name:");
            string name= Console.ReadLine();
            addCmd.Parameters.Add(new OleDbParameter("@Name", name));
            Console.Write("Last Name:");
            string lastname= Console.ReadLine();
            addCmd.Parameters.Add(new OleDbParameter("@LastName", lastname));
            Console.WriteLine();
            addCmd.ExecuteNonQuery();
            con.Close();

        }

```

I changed it to look like this, and now it works, anyway thanks for responses