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. Managed C++/CLI
  4. Write to Access Database using C++ and OleDB connection

Write to Access Database using C++ and OleDB connection

Scheduled Pinned Locked Moved Managed C++/CLI
databasecsharpc++visual-studiocom
9 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.
  • S Offline
    S Offline
    Stefan Baens
    wrote on last edited by
    #1

    I am trying to put information in an access database file (mdb) with an oledb connection. Everything seems fine but no new information is added to the database at the end. This is the code I have now:

    double Tabel_Panelinfo(String^ item_id, String^ use, String^ type)
    {
    // returnwaarde initialiseren
    int dblID = 10;

    // Aanmaken logische naam voor connectie
    OleDbConnection^ myOleDbConnection = nullptr;
    try
    {
        // 1. Build OLE DB connection string for MS Access database
        OleDbConnectionStringBuilder^ connectionBuilder = gcnew OleDbConnectionStringBuilder();
        connectionBuilder->Provider = "Microsoft.Jet.OLEDB.4.0";
        connectionBuilder->DataSource = "D:\\\\Data\\\\Visual Studio 2005\\\\Projects\\\\VertexBDXML-RandekSPL728\\\\VertexBDXML-RandekSPL728\\\\Tabel-XML.mdb";
        
        // 2. Create OLE DB connection
        myOleDbConnection = gcnew OleDbConnection(connectionBuilder->ToString());
        
        // 3. Open OLE DB connection
        myOleDbConnection->Open();
    
    	// 4. create OLE DB DataAdapter
    	OleDbDataAdapter^ DataAdapter = gcnew OleDbDataAdapter("SELECT \* FROM tblPanel", myOleDbConnection);
    
        // 4a. Attach FillError and RowUpdated event handlers
        DataAdapter->FillError += gcnew FillErrorEventHandler(MyEventHandler::FillError);
        DataAdapter->RowUpdated += gcnew OleDbRowUpdatedEventHandler(MyEventHandler::RowUpdated);
    
        // 5. Create DataSet
        DataSet^ myDataSet = gcnew DataSet();
    
    	// 6. Dataset opvullen
    	DataAdapter->MissingSchemaAction = MissingSchemaAction::Add;
        DataAdapter->Fill(myDataSet, "tblPanel");
    
    	// 7. Waarde zoeken
    	DataRow^ row = myDataSet->Tables\["tblPanel"\]->NewRow();
    	// DataRow^ row = myDataSet->Tables\["tblPanel"\]->Rows->Find("1");
    	if (row != nullptr)
    	{
    		// 8. Datavelden invullen
    		// row\["ID"\] = dblID;
    		row\["item\_id"\] = item\_id;
    		row\["use"\] = use;
    		row\["type"\] = Convert::ToInt16(type);
    		// dblID = Convert::ToDouble(row\["ID"\]);
    
    		// 9. Data updaten
    		DataAdapter->Update(myDataSet, "tblPanel");
    	}
    }
    catch(Exception^ e)
    {
    	Console::WriteLine(e->Message);
    }
    finally
    {
        // Close OLE DB connection
        if ( myOleDbConnection != nullptr )
            myOleDbConnection->Close();
    }
    
    return dblID;
    

    }

    Stefan

    L 2 Replies Last reply
    0
    • S Stefan Baens

      I am trying to put information in an access database file (mdb) with an oledb connection. Everything seems fine but no new information is added to the database at the end. This is the code I have now:

      double Tabel_Panelinfo(String^ item_id, String^ use, String^ type)
      {
      // returnwaarde initialiseren
      int dblID = 10;

      // Aanmaken logische naam voor connectie
      OleDbConnection^ myOleDbConnection = nullptr;
      try
      {
          // 1. Build OLE DB connection string for MS Access database
          OleDbConnectionStringBuilder^ connectionBuilder = gcnew OleDbConnectionStringBuilder();
          connectionBuilder->Provider = "Microsoft.Jet.OLEDB.4.0";
          connectionBuilder->DataSource = "D:\\\\Data\\\\Visual Studio 2005\\\\Projects\\\\VertexBDXML-RandekSPL728\\\\VertexBDXML-RandekSPL728\\\\Tabel-XML.mdb";
          
          // 2. Create OLE DB connection
          myOleDbConnection = gcnew OleDbConnection(connectionBuilder->ToString());
          
          // 3. Open OLE DB connection
          myOleDbConnection->Open();
      
      	// 4. create OLE DB DataAdapter
      	OleDbDataAdapter^ DataAdapter = gcnew OleDbDataAdapter("SELECT \* FROM tblPanel", myOleDbConnection);
      
          // 4a. Attach FillError and RowUpdated event handlers
          DataAdapter->FillError += gcnew FillErrorEventHandler(MyEventHandler::FillError);
          DataAdapter->RowUpdated += gcnew OleDbRowUpdatedEventHandler(MyEventHandler::RowUpdated);
      
          // 5. Create DataSet
          DataSet^ myDataSet = gcnew DataSet();
      
      	// 6. Dataset opvullen
      	DataAdapter->MissingSchemaAction = MissingSchemaAction::Add;
          DataAdapter->Fill(myDataSet, "tblPanel");
      
      	// 7. Waarde zoeken
      	DataRow^ row = myDataSet->Tables\["tblPanel"\]->NewRow();
      	// DataRow^ row = myDataSet->Tables\["tblPanel"\]->Rows->Find("1");
      	if (row != nullptr)
      	{
      		// 8. Datavelden invullen
      		// row\["ID"\] = dblID;
      		row\["item\_id"\] = item\_id;
      		row\["use"\] = use;
      		row\["type"\] = Convert::ToInt16(type);
      		// dblID = Convert::ToDouble(row\["ID"\]);
      
      		// 9. Data updaten
      		DataAdapter->Update(myDataSet, "tblPanel");
      	}
      }
      catch(Exception^ e)
      {
      	Console::WriteLine(e->Message);
      }
      finally
      {
          // Close OLE DB connection
          if ( myOleDbConnection != nullptr )
              myOleDbConnection->Close();
      }
      
      return dblID;
      

      }

      Stefan

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      This is the managed C++ forum (as clearly stated on this page) and therefore the wrong forum for this question. Make sure you are using some sort of tutorial or example, there are many database articles here on code project (use the site navigation ). When you repost your question in the correct forum (Visual C++ / MFC ) be sure to reference the article or tutorial you are working from.

      I 1 Reply Last reply
      0
      • L led mike

        This is the managed C++ forum (as clearly stated on this page) and therefore the wrong forum for this question. Make sure you are using some sort of tutorial or example, there are many database articles here on code project (use the site navigation ). When you repost your question in the correct forum (Visual C++ / MFC ) be sure to reference the article or tutorial you are working from.

        I Offline
        I Offline
        iddqd515
        wrote on last edited by
        #3

        he's in the right forum, he's clearly using C++/CLI and not native C++.

        L 1 Reply Last reply
        0
        • I iddqd515

          he's in the right forum, he's clearly using C++/CLI and not native C++.

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          :laugh::laugh: ooops apparently I have been brainwashed. I went off the subject.

          M 1 Reply Last reply
          0
          • S Stefan Baens

            I am trying to put information in an access database file (mdb) with an oledb connection. Everything seems fine but no new information is added to the database at the end. This is the code I have now:

            double Tabel_Panelinfo(String^ item_id, String^ use, String^ type)
            {
            // returnwaarde initialiseren
            int dblID = 10;

            // Aanmaken logische naam voor connectie
            OleDbConnection^ myOleDbConnection = nullptr;
            try
            {
                // 1. Build OLE DB connection string for MS Access database
                OleDbConnectionStringBuilder^ connectionBuilder = gcnew OleDbConnectionStringBuilder();
                connectionBuilder->Provider = "Microsoft.Jet.OLEDB.4.0";
                connectionBuilder->DataSource = "D:\\\\Data\\\\Visual Studio 2005\\\\Projects\\\\VertexBDXML-RandekSPL728\\\\VertexBDXML-RandekSPL728\\\\Tabel-XML.mdb";
                
                // 2. Create OLE DB connection
                myOleDbConnection = gcnew OleDbConnection(connectionBuilder->ToString());
                
                // 3. Open OLE DB connection
                myOleDbConnection->Open();
            
            	// 4. create OLE DB DataAdapter
            	OleDbDataAdapter^ DataAdapter = gcnew OleDbDataAdapter("SELECT \* FROM tblPanel", myOleDbConnection);
            
                // 4a. Attach FillError and RowUpdated event handlers
                DataAdapter->FillError += gcnew FillErrorEventHandler(MyEventHandler::FillError);
                DataAdapter->RowUpdated += gcnew OleDbRowUpdatedEventHandler(MyEventHandler::RowUpdated);
            
                // 5. Create DataSet
                DataSet^ myDataSet = gcnew DataSet();
            
            	// 6. Dataset opvullen
            	DataAdapter->MissingSchemaAction = MissingSchemaAction::Add;
                DataAdapter->Fill(myDataSet, "tblPanel");
            
            	// 7. Waarde zoeken
            	DataRow^ row = myDataSet->Tables\["tblPanel"\]->NewRow();
            	// DataRow^ row = myDataSet->Tables\["tblPanel"\]->Rows->Find("1");
            	if (row != nullptr)
            	{
            		// 8. Datavelden invullen
            		// row\["ID"\] = dblID;
            		row\["item\_id"\] = item\_id;
            		row\["use"\] = use;
            		row\["type"\] = Convert::ToInt16(type);
            		// dblID = Convert::ToDouble(row\["ID"\]);
            
            		// 9. Data updaten
            		DataAdapter->Update(myDataSet, "tblPanel");
            	}
            }
            catch(Exception^ e)
            {
            	Console::WriteLine(e->Message);
            }
            finally
            {
                // Close OLE DB connection
                if ( myOleDbConnection != nullptr )
                    myOleDbConnection->Close();
            }
            
            return dblID;
            

            }

            Stefan

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            See if adding the row to the table does it. myDataSet->Tables["tblPanel"]->Rows->Add(row);

            S 1 Reply Last reply
            0
            • L led mike

              :laugh::laugh: ooops apparently I have been brainwashed. I went off the subject.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              :laugh: If it makes you feel any better, reading the OP's post first on the Visual C++/MFC board, where he was told he was on the wrong board, then reading the same post here, where he gets told the same thing, was very entertaining :) :beer:

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              L 1 Reply Last reply
              0
              • M Mark Salsbery

                :laugh: If it makes you feel any better, reading the OP's post first on the Visual C++/MFC board, where he was told he was on the wrong board, then reading the same post here, where he gets told the same thing, was very entertaining :) :beer:

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                I might be the only member stupid enough to pull that off! Happy Friday... I'm out of here :beer:

                1 Reply Last reply
                0
                • L led mike

                  See if adding the row to the table does it. myDataSet->Tables["tblPanel"]->Rows->Add(row);

                  S Offline
                  S Offline
                  Stefan Baens
                  wrote on last edited by
                  #8

                  This still does not work, i added it in between the other code. I now get exception for e { "Index was outside the bounds of the array."} Is there maybe a good example to write information to an access database using C++. I have found many examples but cannot find any that works in my project.

                  	DataRow^ row = myDataSet->Tables\["tblPanel"\]->NewRow();
                  	myDataSet->Tables\["tblPanel"\]->Rows->Add(row);
                  	if (row != nullptr)
                  	{
                  		// 8. Datavelden invullen
                  		row\["ID"\] = dblID;
                  		row\["item\_id"\] = item\_id;
                  		row\["use"\] = use;
                  		row\["type"\] = Convert::ToInt16(type);
                  		// dblID = Convert::ToDouble(row\["ID"\]);
                  
                  		// 9. Data updaten
                  		DataAdapter->Update(myDataSet, "tblPanel");
                  	}
                  

                  Stefan

                  S 1 Reply Last reply
                  0
                  • S Stefan Baens

                    This still does not work, i added it in between the other code. I now get exception for e { "Index was outside the bounds of the array."} Is there maybe a good example to write information to an access database using C++. I have found many examples but cannot find any that works in my project.

                    	DataRow^ row = myDataSet->Tables\["tblPanel"\]->NewRow();
                    	myDataSet->Tables\["tblPanel"\]->Rows->Add(row);
                    	if (row != nullptr)
                    	{
                    		// 8. Datavelden invullen
                    		row\["ID"\] = dblID;
                    		row\["item\_id"\] = item\_id;
                    		row\["use"\] = use;
                    		row\["type"\] = Convert::ToInt16(type);
                    		// dblID = Convert::ToDouble(row\["ID"\]);
                    
                    		// 9. Data updaten
                    		DataAdapter->Update(myDataSet, "tblPanel");
                    	}
                    

                    Stefan

                    S Offline
                    S Offline
                    Stefan Baens
                    wrote on last edited by
                    #9

                    I finally found the error that prevented me from adding the data. I have used this code:

                    DataTable^ MyTable = myDataSet->Tables[0];
                    DataRow^ row = MyTable->Rows->Add();

                    instead of this:

                    DataRow^ row = myDataSet->Tables["tblPanel"]->NewRow();

                    Stefan

                    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