1

I've searched as much as I can and can't find anything to help me. So what I have is a script that reads/splits and stores data from a .txt file into some arrays. (The one listed here is Vndnbr). What I'm having trouble with is how to go about inputting each entry in the array as an entry under a column in a MS Access table? This is what I have so far:

public void AddToDatabase()
{
    OleDbCommand command;
    OleDbConnection connection =
        new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" +
                            "Data Source=filepath");
    foreach (string x in Vndnbr)
    {
        cmdstringVND[k] = "insert into Table1 (Vndnbr) Values (x)";
        k++;
        command = OleDbCommand(cmdstringVND[k],connection);
    }

    command.Parameters.AddWithValue("?", ReadFromFile("filepath"));
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
}

I'm not familiar with the Access library or what should be inserted in the first parameter of AddwithValue as I just copy pasted these lines after doing some research. If someone could help me with how to add all the data from an array into a table it would be greatly appreciated, thanks.

2 Answers 2

3

There are many errors in your code

  • In your loop you don't use a parameter to store the value to be inserted
  • You never creare the command. (Use new)
  • You try to execute only the last command because the ExecuteNonQuery is outside the loop

    public void AddToDatabase()
    {
        string cmdText = "insert into Table1 (Vndnbr) Values (?)";
        using(OleDbConnection connection = new OleDbConnection(.....))
        using(OleDbCommand command = new OleDbCommand(cmdText, connection))
        {
            connection.Open();
            command.Parameters.AddWithValue("@p1", "");
            foreach (string x in Vndnbr)
            {
                 command.Parameters["@p1"].Value = x;
                 command.ExecuteNonQuery();
    
            }
        }
    }
    

I have changed you code to include the using statement to correctly close and dispose the connection and the command, then I have initialized the command outside the loop, passed a common string with as a parameter placeholder and initialized this parameter with a dummy value.

Inside the loop I have replaced the previous parameter value with the actual value obtained by your Vndnbr list and executed the command.

Sign up to request clarification or add additional context in comments.

Comments

1

You'll want to change your SQL to this:

"insert into Table1 (Vndnbr) Values (@x)";

and then the AddWithValue is like this:

command.Parameters.AddWithValue("@x", ReadFromFile("filepath"));

All you're doing is saying, for this parameter name, I want this value assigned.

4 Comments

ReadFromFile() isn't being recognized, do you have any reason why this might be?
@JohnSmith, what is ReadFromFile supposed to be? Is that a method you wrote? Is that something you had expected was readily available on the object you're currently in? I don't know what ReadFromFile is.
I was researching how to properly make a connection and copy pasted that block of code. Looking at visual studios comment it says the second parameter is the value.. is @x not the value? Sorry for the confusion
@JohnSmith @x is the name of the parameter. You see that parameter in the modified insert statement I provided. The second parameter is in fact the value. But if you copied and pasted ReadFromFile from the internet, you need to get that method and add it to this class (or to a static class) so that it can be accessed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.