Results 1 to 5 of 5

Thread: adding a record to a dataset using ado.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    10

    adding a record to a dataset using ado.net

    I have opened a connection to an OLEDB using the following code below. However, after the application that uses the dll I created is done running and I open the access database the table does not contain the data I added. Can anyone tell me why?

    ' Open a connection to the database.
    Dim strConnection As String = "DataSource=C:\test.mdb;"
    Dim cn As oledb.oledbconnection = new oledb.oledbconnection(strConnection)
    cn.Open( )

    ' Set up a data set command object.
    Dim strSelect As String = "SELECT * FROM Table"
    Dim cmd As New oledb.oledbAdapter(strSelect, cn)

    Dim ds As New DataSet( )
    dscmd.Fill(ds, "Table")

    dim dt as datatable()
    Dim row As DataRow = dt.NewRow( )
    row("ID") = 1
    row("Description") = "Test 1"
    dt.Rows.Add(row)

    ' Close the connection.
    cn.Close( )

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You have to use the Update method in the DataAdapter.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    And as you are not using a typed dataset you have to use a CommandBuilder object to build the update command before the Update method Edneeis mentioned.

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    do you get a typed dataset from using the fillschema command thing before filling the dataset?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Nope, you get a typed dataset by using the server explorer to drag database items to your form and then use the automaticly created DataAdapters to creat a Typed dataset. A schema file is then created for the dataset and a Dataset is created based on that schema file. There is other ways to do it for what I've read but that is the easiest way for sure, else you have to do alot of that schema coding yourself.

    The building of the Command that I mentioned for a non type dataset is not that hard though.... If you have a DataAdapter calle OledbDataAdapter1 you can do the following....

    Code:
    Dim cmdb As New OleDb.OleDbCommandBuilder(OleDbDataAdapter1)
    OleDbDataAdapter1.UpdateCommand = cmdb.GetUpdateCommand
    Similar goes with the InsertCommand and DeleteCommand of course.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width