CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    May 2011
    Posts
    27

    Error while executing the insert query.

    Its an console window application.I am getting the error (unknown String)while running the below code.Kindly check the error and let me know where i have to change.
    ----------------------------------------------------------------------------------------------------------

    Code:
    #include "stdafx.h"
    #include "objbase.h"
    #include "icrsint.h"
    #include <ole2.h>
    
     #import "C:\Program Files\Common Files\System\ado\msado15.dll" \
     no_namespace rename("EOF", "EndOfFile")
     void Insert();
    
     int main(int argc, char* argv[])
     {
    	    //HRESULT CoInitialize(NULL);
    	    if ( FAILED(::CoInitialize(NULL)) )
                return -1;
                Insert();
    			::CoUninitialize();
    		    getchar();
                return 0;             
     }
    
      void Insert()
    
    {
        /*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;Data Source=DEEPA\\SQLEXPRESS;Initial Catalog=keerth;User ID=sa;Password=tovya;Connect Timeout=30;");
    
         HRESULT hr = S_OK;
    	 //printf("connection string with all correct attributes");
    
         //Initialize the COM Library
         //CoInitialize(NULL);
    
         try
         {   
             //Create the Connection pointer
             hr = pConn.CreateInstance((__uuidof(Connection)));
    		// printf("\n connection and create instance.");
    
             if(FAILED(hr))
             {
                 printf("Error instantiating Connection object\n");
                 goto cleanup;
             }
    
             //Open the SQL Server connection
    		
             hr = pConn->Open(strCon,"sa","tovya",0);
    		 
             if(FAILED(hr))
             {
                  printf("Error Opening Database object using ADO _ConnectionPtr \n");
                  goto cleanup;
             }
    
             /*Initialize the values */
            strName = "'C++ADOinsert',";
            strAge = "23,";
    		strDOB = "'13/04/1988 12:0:0',";
            strSalary = "1660)";
    
            /* Append the values to the Insert Statement */
    		 //printf("\n connection with openString");
    		 _bstr_t strSQL("Insert into table1(NAMES,AGE,DOB,SALARY) Values(");
    
            strSQL += strName + strAge + strDOB + strSalary ;
    
            printf("&#37;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());
    		pConn->Close();
    
         }
        cleanup:
        CoUninitialize();
    
    }
    Last edited by ovidiucucu; December 22nd, 2011 at 04:47 AM. Reason: Added [CODE] tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured