We help IT Professionals succeed at work.

how to create insert command into access db

MarkH12518
MarkH12518 asked
on
86 Views
Last Modified: 2016-02-12
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

Comment
Watch Question

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
CERTIFIED EXPERT
Top Expert 2015

Commented:
how about using SQL to append a record?

INSERT INTO tablename (fieldname1, fieldname2, ...)
SELECT value1, value2, ...
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Every table should have a primary key even if it is an autonumber.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.