Link to home
Start Free TrialLog in
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

Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

how about using SQL to append a record?

INSERT INTO tablename (fieldname1, fieldname2, ...)
SELECT value1, value2, ...
ASKER CERTIFIED SOLUTION
Avatar of MohitPandit
MohitPandit
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Every table should have a primary key even if it is an autonumber.