3

I want to insert a new row into an Access database. I'm looking at doing something like:

oConnection = new Connection("connectionstring")
oTable = oCennection.table("Orders")
oRow = oTable.NewRow
oRow.field("OrderNo")=21
oRow.field("Customer") = "ABC001"
oTable.insert

Which seems to be a sensible way of doing things to me.

However, All examples I look for on the net seem to insert data by building SQL statements, or by creating a "SELECT * From ...", and then using this to create a multitude of objects, one of which appears to allow you to ...
- populate an array with the current contents of the table.
- insert a new row into this array.
- update the database with the changes to the array.

What's the easiest way of using vb.net to insert data into an Access database?
Is there a method I can use that is similar to my pCode above?

1
  • Are you running your code in Access? If not, then your question is not an Acccess question at all, since all you're using is a Jet database. Commented Dec 5, 2008 at 3:53

3 Answers 3

2

This is one way:

cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;")
cn.Open()
str = "insert into table1 values(21,'ABC001')"
cmd = New OleDbCommand(str, cn)
cmd.ExecuteNonQuery

I would make a dataset, add a tableadapter connected to the Access database, then let the tableadapter create update/delete/modify for me. Then you just could do like this (assuming your database has a usertable and you mapped that up in the dataset):

    Dim UserDS As New UserDS
    Dim UserDA As New UserDSTableAdapters.UsersTableAdapter
    Dim NewUser As UserDS.UsersRow = UserDS.Users.NewUsersRow

    NewUser.UserName = "Stefan"
    NewUser.LastName = "Karlsson"

    UserDS.User.AddUserRow(NewUser)

    UserDA.Update(UserDS.Users)
Sign up to request clarification or add additional context in comments.

1 Comment

I needed to add "UserDS.User.AddUserRow(NewUser)" before the Update Line to make that work.
1

how to insert the string in vb.net only insert in the integer my pgm is

Try
    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\User\My Documents\db1.mdb;")
    cn.Open()
    str = "insert into table1 values(" & CInt(t2.Text) & ",'" & (t1.Text) & ") "
    cmd = New OleDbCommand(str, cn)
    icount = cmd.ExecuteNonQuery
    MessageBox.Show("stored")
Catch
End Try
cn.Close()

Comments

-1

Unfortunately, the world doesn't re-arrange itself to suit how you think it should work. If you want to work with a database, it's a good idea to get at least somewhat comfortable with SQL, as that is generally how it's done.

On the bright side, there is a whole category of products that do kind of what you want. You can look for an Object-Relational Mappings (ORM) tool like ActiveRecord, NHibernate, or LINQ. But still learn some SQL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.