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: est.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( )

Comments

  • :
    :
    : 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: est.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( )
    :
    :

    Your code only adds a row to the dataset. The underlying datasource is unaffected until you issue an update method ... below is the modifies code ... I commeneted where i made the changes ...

    [code]
    ' Open a connection to the database.
    Dim strConnection As String = "DataSource=C: est.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.OleDbDataAdapter(strSelect, cn)

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

    ' FORGOT TO SAVE REFERENCE TO THE TABLE
    Dim dt As DataTable = ds.Tables("Table")

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

    ' NEED TO ISSUE AN UPDATE USING THE DATA ADAPTER
    cmd.Update(ds, "Table")

    ' Close the connection.
    cn.Close()
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion