OleDbDataAdapter question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Son Ha

    OleDbDataAdapter question


    I want to copy some record from a Access database to another Access DB. My
    code as follow but not working. The destAdapter.Upd ate() return 0 record
    affected.
    Tell me what's wrong in my code?

    public void CopyDataSet(int start, int end)
    {
    OleDbDataAdapte r sourceAdapter = new OleDbDataAdapte r("SELECT * FROM [nguoi
    su dung dat] " +
    "WHERE ID > " + (start - 1).ToString() +
    " AND [nguoi su dung dat].id < " + (end + 1).ToString(), connectionStrin g
    + sourceMDB);
    DataSet sourceDataSet = new DataSet("nguoi su dung dat");
    sourceAdapter.F ill(sourceDataS et);

    OleDbDataAdapte r destAdapter = new OleDbDataAdapte r("SELECT * FROM [nguoi
    su dung dat]",
    connectionStrin g + destinationMDB) ;
    OleDbCommandBui lder builder = new OleDbCommandBui lder(destAdapte r);
    DataSet destDataSet = sourceDataSet.C lone();
    destDataSet.Mer ge(sourceDataSe t); // Same as sourceDataSet
    destAdapter.Upd ate(destDataSet ); // Nothing updated :(
    }

    Thanks


    --

    XP Pro SP2, .NET v1.1, VS.NET 2003


  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: OleDbDataAdapte r question

    Hi,

    I would rather use a DataReader to read the records and at the same time
    inserting these records in the second DB.

    Cheers,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation



    "Son Ha" <sonha79@hotmai l.com> wrote in message
    news:b879077400 a48c75f5f0c8a88 02@news.microso ft.com...[color=blue]
    >
    > I want to copy some record from a Access database to another Access DB. My
    > code as follow but not working. The destAdapter.Upd ate() return 0 record
    > affected.
    > Tell me what's wrong in my code?
    >
    > public void CopyDataSet(int start, int end)
    > {
    > OleDbDataAdapte r sourceAdapter = new OleDbDataAdapte r("SELECT * FROM
    > [nguoi su dung dat] " +
    > "WHERE ID > " + (start - 1).ToString() +
    > " AND [nguoi su dung dat].id < " + (end + 1).ToString(), connectionStrin g
    > + sourceMDB);
    > DataSet sourceDataSet = new DataSet("nguoi su dung dat");
    > sourceAdapter.F ill(sourceDataS et);
    >
    > OleDbDataAdapte r destAdapter = new OleDbDataAdapte r("SELECT * FROM [nguoi
    > su dung dat]",
    > connectionStrin g + destinationMDB) ;
    > OleDbCommandBui lder builder = new OleDbCommandBui lder(destAdapte r);
    > DataSet destDataSet = sourceDataSet.C lone();
    > destDataSet.Mer ge(sourceDataSe t); // Same as sourceDataSet
    > destAdapter.Upd ate(destDataSet ); // Nothing updated :(
    > }
    >
    > Thanks
    >
    >
    > --
    >
    > XP Pro SP2, .NET v1.1, VS.NET 2003
    >
    >[/color]


    Comment

    • Bart Mermuys

      #3
      Re: OleDbDataAdapte r question

      Hi,
      [inline]

      "Son Ha" <sonha79@hotmai l.com> wrote in message
      news:b879077400 a48c75f5f0c8a88 02@news.microso ft.com...[color=blue]
      >
      > I want to copy some record from a Access database to another Access DB. My
      > code as follow but not working. The destAdapter.Upd ate() return 0 record
      > affected.
      > Tell me what's wrong in my code?[/color]

      The problem is that after a normal Fill operation all rowstates will be
      "unmodified ", merging it with the dest dataset doesn't change their state,
      so when you update nothing happens because all rows are marked as
      "unmodified ".

      So you have to make sure that the rows have an "added" rowstate :

      1) Before you call "sourceAdapter. Fill(sourceData Set)", you first set
      sourceAdapter.A cceptChangesDur ingFill = false;

      --- OR ---

      2) Manually insert each row using BeginLoadData, LoadDataRow & EndLoadData
      something like:

      destTable.Begin LoadData();
      foreach ( DataRow dr in sourceTable.Row s )
      destTable.LoadD ataRow( dr.ItemArray, false );
      destTable.EndLo adData();


      HTH,
      Greetings

      [color=blue]
      >
      > public void CopyDataSet(int start, int end)
      > {
      > OleDbDataAdapte r sourceAdapter = new OleDbDataAdapte r("SELECT * FROM
      > [nguoi su dung dat] " +
      > "WHERE ID > " + (start - 1).ToString() +
      > " AND [nguoi su dung dat].id < " + (end + 1).ToString(), connectionStrin g
      > + sourceMDB);
      > DataSet sourceDataSet = new DataSet("nguoi su dung dat");
      > sourceAdapter.F ill(sourceDataS et);
      >
      > OleDbDataAdapte r destAdapter = new OleDbDataAdapte r("SELECT * FROM [nguoi
      > su dung dat]",
      > connectionStrin g + destinationMDB) ;
      > OleDbCommandBui lder builder = new OleDbCommandBui lder(destAdapte r);
      > DataSet destDataSet = sourceDataSet.C lone();
      > destDataSet.Mer ge(sourceDataSe t); // Same as sourceDataSet
      > destAdapter.Upd ate(destDataSet ); // Nothing updated :(
      > }
      >
      > Thanks
      >
      >
      > --
      >
      > XP Pro SP2, .NET v1.1, VS.NET 2003
      >
      >[/color]


      Comment

      Working...