Inserting to an access database via visual studio

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evilash
    New Member
    • Mar 2008
    • 4

    Inserting to an access database via visual studio

    Hi,

    am creating a web form in visual studio to insert data into an access database. The web form text boxes on the aspx file are Textbox.Text, Textbox2.Text and Textbox4.Text. The info is going to an access database i have in my app_data folder called users.mdb. The field names are Username, Password, Email_Address. The textbox data is being sent in the same order to the fields and textbox order listed above. Am fairly new to visual studio and asp.net. What code is required to submit this information to the access database.

    Thanks
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    First up you need to make sure you have some imports in your codebehind at the top of your class so that you can reference your access database:

    Imports System.Data
    Imports System.Data.Ole db

    The code is relatively simple
    Code:
    Dim oCon As New OledbConnection("ConnectionStringToAccessDB")
    oCon.Open()
    Dim oCmd As New OledbCommand("Insert into MyTable(Field1, Field2, Field3) Values @Value1, @Value2, @Value3", oCon)
    oCmd.Parameters.Add(New OledbParameter("Value1", "ValueToEnterIntoField1"))
    oCmd.Parameters.Add(New OledbParameter("Value2", "ValueToEnterIntoField2"))
    oCmd.Parameters.Add(New OledbParameter("Value3", "ValueToEnterIntoField3"))
    oCmd.ExecuteNonQuery()
    oCmd.Dispose()
    oCon.Close
    oCon.Dispose

    Comment

    Working...