locked
How to create an Access DB? RRS feed

  • Question

  • I want to write an Access Database 2007/2010 (a file accdb). To read the database I use the OleDbConnection andOleDbDataReader classes, but I cant found the equivalent class to write the DB. 

    I want an Acces DB like the following:

    Country(string) | Phone (string)
    ---------------------------------
    France                 15334864
    Japan                  884556

    How can I do?


    • Edited by doh666 Saturday, June 23, 2012 10:48 AM
    Saturday, June 23, 2012 10:38 AM

Answers

  • Hi Doh666,

    Welcome to the MSDN Forum.

    To write record into a database, you need the insert sql command: http://msdn.microsoft.com/en-us/library/ms174335.aspx 

    And here is an example about insert an Access database:

        Sub InsertAccessDB(data()() As Array)
    
            Dim con As New OleDbConnection
            Dim cmd As New OleDbCommand
    
            con.ConnectionString = "provider=Microsoft.ACE.OLEDB.12.0;data source=D:\DB\Database11.accdb;"
            cmd.Connection = con
    
            cmd.CommandText = "INSERT INTO yourTableName (employeenumber, exceptiondate, minutes, code) VALUES (@en,@ed,@min,@code)"
            For i = 0 To data.GetLength(1)
                cmd.Parameters.Clear()
                With cmd.Parameters
                    .Add("@en", OleDbType.Char).Value = data(i)(0)
                    .Add("@ed", OleDbType.DBTimeStamp).Value = data(i)(1)
                    .Add("@min", OleDbType.Char).Value = data(i)(2)
                    .Add("@code", OleDbType.Char).Value = data(i)(3)
                End With
                cmd.ExecuteNonQuery()
            Next
    
        End Sub

    If you need the C# code, you can translate it by this online tool: http://www.developerfusion.com/tools/convert/vb-to-csharp/ 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Sunday, July 8, 2012 9:27 AM
    Thursday, June 28, 2012 9:41 AM

All replies

  • Saturday, June 23, 2012 4:21 PM
  • a databae always has a last row which is blank for adding new data.  So to write data to a database after conneting do the following

    1) Move to last row

    2) Write data to each of the fields

    3) Perform an update.  The update will save the data and create a new empty row.

    See Update methods on this webpage.


    jdweng

    Saturday, June 23, 2012 4:41 PM
  • Did you know that this is the forum for System Base, (which is the base class of .Net) and related questions.

    Your question can be typical for the System.Data forums, however in fact is your question more a program language question as it is direct related to that.

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral


    Success
    Cor

    Monday, June 25, 2012 5:38 AM
  • Hi Doh666,

    Welcome to the MSDN Forum.

    To write record into a database, you need the insert sql command: http://msdn.microsoft.com/en-us/library/ms174335.aspx 

    And here is an example about insert an Access database:

        Sub InsertAccessDB(data()() As Array)
    
            Dim con As New OleDbConnection
            Dim cmd As New OleDbCommand
    
            con.ConnectionString = "provider=Microsoft.ACE.OLEDB.12.0;data source=D:\DB\Database11.accdb;"
            cmd.Connection = con
    
            cmd.CommandText = "INSERT INTO yourTableName (employeenumber, exceptiondate, minutes, code) VALUES (@en,@ed,@min,@code)"
            For i = 0 To data.GetLength(1)
                cmd.Parameters.Clear()
                With cmd.Parameters
                    .Add("@en", OleDbType.Char).Value = data(i)(0)
                    .Add("@ed", OleDbType.DBTimeStamp).Value = data(i)(1)
                    .Add("@min", OleDbType.Char).Value = data(i)(2)
                    .Add("@code", OleDbType.Char).Value = data(i)(3)
                End With
                cmd.ExecuteNonQuery()
            Next
    
        End Sub

    If you need the C# code, you can translate it by this online tool: http://www.developerfusion.com/tools/convert/vb-to-csharp/ 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Sunday, July 8, 2012 9:27 AM
    Thursday, June 28, 2012 9:41 AM