C++ ADO Insert Sample

CoderSource.net
C++ ADO Insert Sample
Rating: 4

Date: 5/1/2004 12:00:00 AM

  This following sample code describes how to insert a record into a sample table in SQL Server. The sample code inserts data for each one of the following types.
  • Number
  • Varchar
  • Date
  • Money(currency)        

    The sample first creates a connection string for the SQL Server database. It then instantiates the _ConnectionPtr interface, with a call to the CoCreateInstance. Any call to the CoCreateInstance should be preceded with a CoInitialize or CoInitializeEx at least once during the life time of the program.
  The reason behind this call is to initialize the COM libraries. After this call any COM call can be made. After the call to CoCreateInstance, we open the database connection with the connection string.
  The _ConnectionPtr object has the capability to execute an SQL DML statement like Insert, Update and Delete. The sample code uses this _ConnectionPtr object's Execute member function for inserting the record into the table.

  The following sample is a complete program for inserting one record into a table named "Table1". Replace the username, password, databasename,(local) with the local username, password, database name and the server name. There is a similar article named C++ ADO Stored Procedure Using Command Object, which explains how to use a stored procedure in C++ ADO. 


#include <windows.h>
#include <stdio.h>

#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

int main(int argc, char* argv[])
{

    /*The following variables will be initialized with necessary values and  appended to the strSQL values*/
    _bstr_t strName;
    _bstr_t strAge;
    _bstr_t strDOB;
    _bstr_t strSalary;

 
    _ConnectionPtr pConn = NULL;
    // Define string variables for ADO connection
    _bstr_t strCon("Provider=SQLOLEDB.1;Persist Security Info=False;User     ID=username;Password=password;Initial Catalog=databasename;Data Source=(local);Integrated Security=SSPI;");

    HRESULT hr = S_OK;

    //Initialize the COM Library
    CoInitialize(NULL);

    try
    {
        //Create the Connection pointer
        hr = pConn.CreateInstance((__uuidof(Connection)));
        if(FAILED(hr))
        {
            printf("Error instantiating Connection object\n");
            goto cleanup;
        }

        //Open the SQL Server connection
        hr = pConn->Open(strCon,"","",0);
        if(FAILED(hr))
        {
             printf("Error Opening Database object using ADO _ConnectionPtr \n");
             goto cleanup;
        }

        /*Initialize the values */
       strName = "'Codersource C++ ADO insert Sample',";
       strAge = "10,";
       strDOB = "'02/10/2004',";
       strSalary = "1010.10)";

       /* Append the values to the Insert Statement */
      _bstr_t strSQL("Insert into Table1(Name,Age,DOBirth,Salary) Values(");

       strSQL += strName + strAge + strDOB + strSalary ;

       printf("%s\n",(LPCSTR)strSQL);

       //Execute the insert statement
      pConn->Execute(strSQL,NULL,adExecuteNoRecords);

      printf("Data Added Successfully\n",(LPCSTR)strSQL);

     //Close the database
     pConn->Close();

    }
    catch(_com_error & ce)
    {
       printf("Error:%s\n",ce.ErrorInfo);
    }


cleanup:
    CoUninitialize();

    return 0;
}
 

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us