Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,083 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,537 people online right now. Registration is fast and FREE... Join Now!




Insert into a MS ACCESS file

 
Reply to this topicStart new topic

Insert into a MS ACCESS file

vyomaraju
22 Apr, 2008 - 06:27 PM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2008
Posts: 3

hi sir

I just wanted to know how do i insert into a data base. i have attached my code and the DSN name to this is testMDB please help to add in the records as soon as possible

Regards
Vyoma


Attached File(s)
Attached File  MDB_CPP_Application.zip ( 59.21k ) Number of downloads: 24
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Insert Into A MS ACCESS File
22 Apr, 2008 - 08:42 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks smile.gif
User is online!Profile CardPM
+Quote Post

vyomaraju
RE: Insert Into A MS ACCESS File
22 Apr, 2008 - 09:08 PM
Post #3

New D.I.C Head
*

Joined: 16 Apr, 2008
Posts: 3

QUOTE(PsychoCoder @ 22 Apr, 2008 - 09:42 PM) *

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks smile.gif



hi sir

CODE


// database.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>

#include "database.h"




class Database
{
public:
   //CnnPtr m_Cnn;
    ADODB::_ConnectionPtr m_Con;
   Database();
   ~ Database();
   bool Open(char* UserName, char* Pwd, char* CnnStr/*, char* ErrStr = NULL*/);
   //RecPtr Execute(char* CmdStr);
   ADODB::_RecordsetPtr Execute(char* CmdStr);
   bool Close();
};
Database::Database()
{
   m_Con=NULL;
}


bool Database::Open(char* UserName, char* Pwd, char* CnnStr)
{

   HRESULT hr;
   try
   {
      //if(m_Con==NULL)
        // return 0;

      //INITIALIZE COM
      CoInitialize(NULL);
      hr    = m_Con.CreateInstance(__uuidof( ADODB::Connection ) );
      m_Con->Open(CnnStr, UserName, Pwd, NULL);

   }
   catch(_com_error &e)
   {
      //   Handle errors
      return 0;
   }
   return 1;
}

//RecPtr Database::Execute(char* CmdStr)
ADODB::_RecordsetPtr  Database::Execute(char* CmdStr)
{
   try
   {
      if(m_Con==NULL)
         return NULL;
      return m_Con->Execute(CmdStr,NULL,1);
   }
   catch(_com_error &e)
   {
      //   Handle errors
      return NULL;
   }
}


bool Database::Close()
{
   //is there any established connection?
   //if no, return 0
    HRESULT hr;

   if (m_Con==NULL)
      return 0;
   //if any, close that connection
   try
   {
      m_Con->Close();
      m_Con=NULL;
   }
   catch(_com_error &e)
   {
      //   Handle errors
      return 0;
   }
   return hr==S_OK;
}


Database::~Database()
{
   try
   {
   //close connection, if not yet closed
      if (m_Con)
      {
         m_Con->Close();
         m_Con=NULL;
      }
   }
   catch(_com_error &e)
   {
      //   Handle errors
   }
}


int main(int argc, char* argv[])
{
    long noOfFields = 0;
    int nIndex = 0;
    char csTemp[1024] = {0, };
    VARIANT varData, varValue;


    //CREATE THE DB VARIABLE
    Database VyomaDB;

    //CREATE THE RECORDSET VARIABLE
    ADODB::_RecordsetPtr VyomaRecordset = NULL;
    
    //OPEN THE DB
    if( VyomaDB.Open("", "", "Provider=MSDASQL.1;Persist Security Info=True;DSN=testMDB") )
    {
        //DB OPENED
        printf("DB Opened");
    }
    else
    {
        printf("Sorry, DB could not be opened!! Try some other conenction String");
    }
    
    
    VyomaDB.Open("", "", "Provider=MSDASQL.1;Persist Security Info=True;DSN=testMDB");



    VyomaRecordset = VyomaDB.Execute("select * from emp_table");
    if(VyomaRecordset == NULL)
        return -1;


    VyomaRecordset->Fields->get_Count(& noOfFields);

    
    while (true) //REPLACE THIS EXPRESSION BY EOF chk if EOF HAS BEEN rEACHED OR NOT.
    {
          // Traversing through the Fields collection to get the values.
            for(nIndex = 0; nIndex < noOfFields; nIndex++)
            {
                if(csTemp[0] == '\0')
                    strcpy(csTemp, "The records are: \n");
            //    else
            //        strcpy(csTemp, "The Next Record is: \n");
              
                strcat(csTemp, VyomaRecordset->Fields->GetItem(_variant_t((long)nIndex))->Name );    
                strcat(csTemp, " = ");
                varValue = VyomaRecordset->Fields->GetItem(_variant_t((long)nIndex))->Value;

              if(varValue.vt == VT_BSTR)
              {
                  strcat(csTemp, _com_util::ConvertBSTRToString(varValue.bstrVal));
                      
              }
              else
              if(varValue.vt == VT_I4)
              {
                  char temp[1024] = {0, };
                  sprintf(temp, "%d\n", varValue.iVal);
                  strcat(csTemp, temp);
              }

      } // End of For loop.
     // MessageBox(NULL, csTemp, "Record", 0);
      
      VyomaRecordset->MoveNext();
      printf("%s\n",csTemp);
          
    

    }
    return 0;
}


This is my code i wanted to enter the data into my MS Access database... so plz tell me how to do tat i tried doin
CODE

VyomaDB.Open("", "", "Provider=MSDASQL.1;Persist Security Info=True;DSN=testMDB");



    VyomaRecordset = VyomaDB.Execute("Insert into emp_table values(3,abc");


but it doesn't update... so please help in addin data into d db
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month