Write to Access Database using C++ and OleDB connection
-
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
-
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
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.
-
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 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
-
: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:
-
: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:
-
See if adding the row to the table does it.
myDataSet->Tables["tblPanel"]->Rows->Add(row);
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
-
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
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