Avatar of MarkH12518
MarkH12518 asked on

how to create insert command into access db

I now have a datatable that matches my access db fields and am able to fill the datatable from arrays in my c#. It is 200 X 242.

Now that I have the datatable I want to insert the rows of data, either one by one or batch into access. I attempted the OleCommandBuilder but do not have a primary key in the table. So I'm doing manually I suppose.

Can someone please indicate how I can Insert into my Access table?

Thank you




DataRow row;
            int ln = dtAr.Length;
            //fill rows
            try
            {
                for (int i = 0; i < sims; i++)
                {
                    row = prtab.NewRow();
                    for (int j = 0; j < ln + 2; j++)
                    {
                        if (j == 0)
                        {
                            row["tStep"] = 1;
                        }
                        else if (j == 1)
                        {
                            row["SimNum"] = i;
                        }
                        else
                        {
                            row[CrvID[j - 2].ToString() + dtAr[j - 2].ToString()] = crNs[i, j - 2];
                        }
                    }

                    prtab.Rows.Add(row);
                }
            }

Open in new window

Microsoft AccessC#

Avatar of undefined
Last Comment
PatHartman

8/22/2022 - Mon
crystal (strive4peace) - Microsoft MVP, Access

how about using SQL to append a record?

INSERT INTO tablename (fieldname1, fieldname2, ...)
SELECT value1, value2, ...
ASKER CERTIFIED SOLUTION
MohitPandit

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PatHartman

Every table should have a primary key even if it is an autonumber.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy