How do I insert a record into an Access database?

Last post 07-04-2008 3:00 PM by Mikesdotnetting. 4 replies.

Sort Posts:

  • How do I insert a record into an Access database?

    06-30-2008, 10:40 AM
    • Loading...
    • Mainship
    • Joined on 01-03-2007, 4:54 PM
    • New York
    • Posts 1,074

    All the Google results talk about inserting data from forms, or gridview controls, or detailsview controls, etc.  But what I need to do is copy over records from a Sql Server database.  Like all the records where [adends] = "6/1/2008"

    I'm using a dataset to find the records I need.  Now. as I 'walk through' the records the dataset returns, I want to copy them to the Access table.  How do I do that?

    I thought of using bulk upload, bur I've never been able to get that to work.

    Diane

  • Re: How do I insert a record into an Access database?

    06-30-2008, 10:58 AM
    • Loading...
    • mohi88
    • Joined on 02-06-2004, 9:21 AM
    • jordan / Turkey
    • Posts 623

     just a small idea might help.

    try to create a table in access with the same fields as your dataset.

    and after that you can make a loop to copy the records into access table like this

    foreach (DataRow _row in DataSet.Tables[0].Rows)
                {

    string strSql="INSERT INTO table
    (column-1, column-2, ... column-n)
    VALUES
    (_row["firscolumn"], _row["secondcolumn"], ... value-n);
                  // you must add each row to table and run the strSql statement.

    //here you do the insert
                }

    connection.close();

    do not forget to open the connection before your enter the loop and do not open it every time you need to insert!.

    hope this helps 

    Muhanad YOUNIS
    MCSD.NET
    My Blog || My Photos

    [Don't forget to click on Mark as answer on the post that helped you.]
  • Re: How do I insert a record into an Access database?

    06-30-2008, 11:52 AM
    • Loading...
    • Mainship
    • Joined on 01-03-2007, 4:54 PM
    • New York
    • Posts 1,074

    I have the Access database with the same fields.  It's in the App_Data folder.  And I have an AccessDataSource that is configured to use it.  But I don't know how to generate the insert statement.

    Diane

  • Re: How do I insert a record into an Access database?

    07-04-2008, 2:52 PM
    • Loading...
    • mohi88
    • Joined on 02-06-2004, 9:21 AM
    • jordan / Turkey
    • Posts 623

     sorry for the delay;

    please send me your source access db table( it may hold many tables but i'll do you one as a sample). and i'll try to configure it for you. 

    Muhanad YOUNIS
    MCSD.NET
    My Blog || My Photos

    [Don't forget to click on Mark as answer on the post that helped you.]
  • Re: How do I insert a record into an Access database?

    07-04-2008, 3:00 PM

    Which bit are you actually stuck with? 

    Muhaned has explained the principal, and shown how to reference the values from the DataSet - just fire an Insert command for each row in the dataset.  Connecting to Access is identical to Sql Server, except that you use commands and connections from OleDb instead of SqlClient, and the connection string is different: http://www.connectionstrings.com/?carrier=access (use the OleDbConnection sample).

     

    Regards Mike
    [MVP - ASP/ASP.NET]
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter