Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# ADO.net Adapter Update Question.

C# ADO.net Adapter Update Question.

Scheduled Pinned Locked Moved C#
csharpdatabasehelpquestionannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adamzimmer
    wrote on last edited by
    #1

    I'm new to C# and am trying to access and update a Access Database. I've managed to create the database and can see the results in Access. I'm also able to open the Database and load the tables, but when I try to do an update it does not post back my changes. Below is the code with comments where things seem to fail. I do not get an exception, I just don't see the changes in the database file. // This method seems to work fine, it loads the data from the database into the six tables. public void openDatabase(DataSet ds, string filename) { // I have 6 tables in the dataset/database. string[] names = { "Project", "Portfolios", "Equations", "Scenarios", "Reports", "Rules" }; string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename; for (int i = 0; i < names.Length; i++) { string strAccessSelect = "SELECT * FROM " + names[i]; try { myAccessConn = new OleDbConnection(strAccessConn); } catch (Exception ex) { Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message); return; } try { OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn); myDataAdapter = new OleDbDataAdapter(myAccessCommand); myAccessConn.Open(); myDataAdapter.Fill(ds, names[i]); } catch (Exception ex) { Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message); return; } finally { myAccessConn.Close(); } } } // This is the method I'm having trouble with, I sent the Dataset off to another routine which changes some // of the datarows, I can see the changes there in the dataset but update does not write them to disk. public void updateDatabase(DataSet ds) { try { dw = new DebugWindow(); // This opens up a window to display the dataset dw.Show(); dw.dumpDataSet(ds); // This dumps the dataset to the window, the dataset looks correct with my c

    P M D 3 Replies Last reply
    0
    • A adamzimmer

      I'm new to C# and am trying to access and update a Access Database. I've managed to create the database and can see the results in Access. I'm also able to open the Database and load the tables, but when I try to do an update it does not post back my changes. Below is the code with comments where things seem to fail. I do not get an exception, I just don't see the changes in the database file. // This method seems to work fine, it loads the data from the database into the six tables. public void openDatabase(DataSet ds, string filename) { // I have 6 tables in the dataset/database. string[] names = { "Project", "Portfolios", "Equations", "Scenarios", "Reports", "Rules" }; string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename; for (int i = 0; i < names.Length; i++) { string strAccessSelect = "SELECT * FROM " + names[i]; try { myAccessConn = new OleDbConnection(strAccessConn); } catch (Exception ex) { Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message); return; } try { OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn); myDataAdapter = new OleDbDataAdapter(myAccessCommand); myAccessConn.Open(); myDataAdapter.Fill(ds, names[i]); } catch (Exception ex) { Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message); return; } finally { myAccessConn.Close(); } } } // This is the method I'm having trouble with, I sent the Dataset off to another routine which changes some // of the datarows, I can see the changes there in the dataset but update does not write them to disk. public void updateDatabase(DataSet ds) { try { dw = new DebugWindow(); // This opens up a window to display the dataset dw.Show(); dw.dumpDataSet(ds); // This dumps the dataset to the window, the dataset looks correct with my c

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      DataAdapters are very simple; they can only do very simple things. They are really only good for writing quick-and-dirty demoes at Microsoft launch events. Any non-trivial application will quickly become complex enough that a DataAdapter will be of no use -- especially Update. I don't use DataAdapters and neither should you (or anyone else for that matter); they cause more trouble than they're worth. You would be much better off in the long run, learning how to access databases at a lower level. ExecuteScalar, ExecuteNonQuery, DataAdapter.Fill, and DataAdapter.Update all use ExecuteReader (and therefore DataReaders); learn to use DataReaders. (I think part of your problem above may be that myDataAdapter gets changed between calls to openDatabase and updateDatabase. But the whole thing should be rewritten. X| )

      A 1 Reply Last reply
      0
      • A adamzimmer

        I'm new to C# and am trying to access and update a Access Database. I've managed to create the database and can see the results in Access. I'm also able to open the Database and load the tables, but when I try to do an update it does not post back my changes. Below is the code with comments where things seem to fail. I do not get an exception, I just don't see the changes in the database file. // This method seems to work fine, it loads the data from the database into the six tables. public void openDatabase(DataSet ds, string filename) { // I have 6 tables in the dataset/database. string[] names = { "Project", "Portfolios", "Equations", "Scenarios", "Reports", "Rules" }; string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename; for (int i = 0; i < names.Length; i++) { string strAccessSelect = "SELECT * FROM " + names[i]; try { myAccessConn = new OleDbConnection(strAccessConn); } catch (Exception ex) { Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message); return; } try { OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn); myDataAdapter = new OleDbDataAdapter(myAccessCommand); myAccessConn.Open(); myDataAdapter.Fill(ds, names[i]); } catch (Exception ex) { Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message); return; } finally { myAccessConn.Close(); } } } // This is the method I'm having trouble with, I sent the Dataset off to another routine which changes some // of the datarows, I can see the changes there in the dataset but update does not write them to disk. public void updateDatabase(DataSet ds) { try { dw = new DebugWindow(); // This opens up a window to display the dataset dw.Show(); dw.dumpDataSet(ds); // This dumps the dataset to the window, the dataset looks correct with my c

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Oi Adam Listen to what Piebald said, quick and dirty or DO NOT USE THEM. Learn about a Data Access Layer (DAL) and implement one of the many existing ones available here in the articles. Most accomplished developers build there own, I see a lot of new devs using the Enterprise library for their DAL these days.

        1 Reply Last reply
        0
        • A adamzimmer

          I'm new to C# and am trying to access and update a Access Database. I've managed to create the database and can see the results in Access. I'm also able to open the Database and load the tables, but when I try to do an update it does not post back my changes. Below is the code with comments where things seem to fail. I do not get an exception, I just don't see the changes in the database file. // This method seems to work fine, it loads the data from the database into the six tables. public void openDatabase(DataSet ds, string filename) { // I have 6 tables in the dataset/database. string[] names = { "Project", "Portfolios", "Equations", "Scenarios", "Reports", "Rules" }; string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename; for (int i = 0; i < names.Length; i++) { string strAccessSelect = "SELECT * FROM " + names[i]; try { myAccessConn = new OleDbConnection(strAccessConn); } catch (Exception ex) { Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message); return; } try { OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn); myDataAdapter = new OleDbDataAdapter(myAccessCommand); myAccessConn.Open(); myDataAdapter.Fill(ds, names[i]); } catch (Exception ex) { Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message); return; } finally { myAccessConn.Close(); } } } // This is the method I'm having trouble with, I sent the Dataset off to another routine which changes some // of the datarows, I can see the changes there in the dataset but update does not write them to disk. public void updateDatabase(DataSet ds) { try { dw = new DebugWindow(); // This opens up a window to display the dataset dw.Show(); dw.dumpDataSet(ds); // This dumps the dataset to the window, the dataset looks correct with my c

          D Offline
          D Offline
          dxlee
          wrote on last edited by
          #4

          You can listen to the other peoples' advice to use ExecuteNonQuery(), etc. However, I am going to just talk about your piece of code. The adapter maintains four different query commands internally. One of them is the update query you implicitly called when you call myDataAdapter.Update(...). However, in order for it to work, the adapter has to know how to correctly construct the update query internally. What you need to make this program to work is to add the following line to tell the adapter how to construct the query internally: OleDbCommandBuilder builder=new OleDbCommandBuilder(myDataAdapter); Add the above line right after the line: myDataAdapter = new OleDbDataAdapter(myAccessCommand); After that, your Update(...) call should work.

          A 1 Reply Last reply
          0
          • P PIEBALDconsult

            DataAdapters are very simple; they can only do very simple things. They are really only good for writing quick-and-dirty demoes at Microsoft launch events. Any non-trivial application will quickly become complex enough that a DataAdapter will be of no use -- especially Update. I don't use DataAdapters and neither should you (or anyone else for that matter); they cause more trouble than they're worth. You would be much better off in the long run, learning how to access databases at a lower level. ExecuteScalar, ExecuteNonQuery, DataAdapter.Fill, and DataAdapter.Update all use ExecuteReader (and therefore DataReaders); learn to use DataReaders. (I think part of your problem above may be that myDataAdapter gets changed between calls to openDatabase and updateDatabase. But the whole thing should be rewritten. X| )

            A Offline
            A Offline
            adamzimmer
            wrote on last edited by
            #5

            Thank you for your insight. I'll look into using a DataReader. When I first implemented this I actually read the .mdb in binary format and converted it line by line. It seems most books point to the DataAdapters, but as you say these seem only good for the quick and dirty. Thanks.

            1 Reply Last reply
            0
            • D dxlee

              You can listen to the other peoples' advice to use ExecuteNonQuery(), etc. However, I am going to just talk about your piece of code. The adapter maintains four different query commands internally. One of them is the update query you implicitly called when you call myDataAdapter.Update(...). However, in order for it to work, the adapter has to know how to correctly construct the update query internally. What you need to make this program to work is to add the following line to tell the adapter how to construct the query internally: OleDbCommandBuilder builder=new OleDbCommandBuilder(myDataAdapter); Add the above line right after the line: myDataAdapter = new OleDbDataAdapter(myAccessCommand); After that, your Update(...) call should work.

              A Offline
              A Offline
              adamzimmer
              wrote on last edited by
              #6

              Thanks dxlee, I gave this a try but update still does not seem to write to the disk. I tried to cut things down to one table as I think flipping between them is causing problems. I'll look more into the OleDBCommandBuilder. Thanks.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups