Click here to Skip to main content
15,890,825 members
Home / Discussions / Database
   

Database

 
GeneralRe: From Excel to C# Pin
NaturePhoenix22-Feb-11 23:48
NaturePhoenix22-Feb-11 23:48 
GeneralRe: From Excel to C# Pin
R. Giskard Reventlov23-Feb-11 0:06
R. Giskard Reventlov23-Feb-11 0:06 
Questionhow to display the months based on date Pin
vinu.111122-Feb-11 20:00
vinu.111122-Feb-11 20:00 
AnswerRe: how to display the months based on date Pin
Blue_Boy22-Feb-11 22:21
Blue_Boy22-Feb-11 22:21 
QuestionMultipage Report??? [modified] Pin
scorp_scorp22-Feb-11 19:02
scorp_scorp22-Feb-11 19:02 
Questionplease modify the Store Procedure Pin
vinu.111121-Feb-11 23:36
vinu.111121-Feb-11 23:36 
AnswerRe: please modify the Store Procedure Pin
Mycroft Holmes22-Feb-11 0:02
professionalMycroft Holmes22-Feb-11 0:02 
QuestionADO To MS Access Database. Pin
Mike Certini21-Feb-11 19:08
Mike Certini21-Feb-11 19:08 
I am having problems with executing a command object in ADO. The specific problem I have revolves around the proper type for the SQL string. Following examples on the internet, I established it with a _bstr_t. This though is not working with the * VARIANT type that is required in the Execute method. Can someone help me with the proper establishment of the SQL string? Listed below is the complete program:

Here is my error message coming out of the try block:

error C2664: 'ADODB::Command15::Execute' : cannot convert parameter 1 from '_bstr_t' to 'VARIANT *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


#include <stdio.h>
#include <string>
using std::string;

int cyc = 0;

#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","EOFile")
using namespace std;

struct StartOLEProcess{
    StartOLEProcess(  ) {
        ::CoInitialize(NULL);
    }
    ~StartOLEProcess(  ) {
        ::CoUninitialize(  );
    }
} _start_StartOLEProcess;
 
void main(void)
{
    // define our variables which will be used as references to the
    // Connection and Recordset objects
 
    ADODB::_ConnectionPtr  con = NULL;
    ADODB::_RecordsetPtr   rec = NULL;
    ADODB::_CommandPtr	   com = NULL;
    ADODB::_ParameterPtr   par = NULL;
	 
    // create two strings for use with the creation of a Connection
    // and a Recordset object
    
    bstr_t                 sConString;
    bstr_t                 sSQLString;
    
    // create a variable to hold the result to function calls
    
    HRESULT                hr                = S_OK;
    
    // long variable needed for Execute method of Connection object
    
    VARIANT                *vRecordsAffected = NULL;
 
    // create instance of an ADO Connection object, ADO Command object, ADO Parameter object, ADO Record object
    
    hr = con.CreateInstance(__uuidof(ADODB::Connection));
    hr = com.CreateInstance(__uuidof(ADODB::Command));
    hr = par.CreateInstance(__uuidof(ADODB::Parameter));
    hr = rec.CreateInstance(__uuidof(ADODB::Record));
   
    printf("Connection object created.\n");
 
    // open the data source with the Connection object
    
    sConString = L"Provider=Microsoft.Jet.OLEDB.4.0;" 
                 L"Data Source=C:\\Users\\Mike Certini\\Documents\\Trading\\Databases\\TradingAnalysis#2.mdb";
				 //L"DataSchema=mytable";

    // open the connection.
	
    hr = con->Open(sConString, L"", L"", -1);
	
    printf("Connection has been opened.\n");

    // create and store SQL string for command string.
	
    _bstr_t strSQL("INSERT INTO mytable(id,desc)");

    // activate connection string.

    com->ActiveConnection = con;
	
    // establish command string parameters. 

    com->CommandText = strSQL;
    com->CommandType = ADODB::adCmdStoredProc;
		
    // Define Integer/variant.
    VARIANT vtInt;
    int intNum = 1;
    vtInt.vt = VT_I2;
    vtInt.iVal = intNum;

    // Define Character/variant.
    VARIANT vText; 
    vText.vt = VT_BSTR;
    vText.bstrVal = _bstr_t("This is text");

    // Complete parameter objects

    hr = com->Parameters->Append(com->CreateParameter(_bstr_t("id"),ADODB::adInteger,ADODB::adParamInput,4,intNum));
    hr = com->Parameters->Append(com->CreateParameter(_bstr_t("desc"),ADODB::adChar,ADODB::adParamInput,15,vText));

    // Execute command object

    try
	{
	rec = com->Execute(strSQL, NULL, ADODB::adCmdStoredProc);
	}
	catch(_com_error &e)
	{
	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());

	// Print COM errors. 
	printf("Error\n");
	printf("\tCode = %08lx\n", e.Error());
	printf("\tCode meaning = %s\n", e.ErrorMessage());
	printf("\tSource = %s\n", (LPCSTR) bstrSource);
	printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
	}	

    rec->Close(  );
    rec = NULL;

    printf("Closed an removed the "
           "Recordset object from memory.\n");

    // close and remove the Connection object from memory
    
    con->Close(  );
    con = NULL;
	
    printf("Closed and removed the "
		  "Connection object from memory.\n");
}

AnswerRe: ADO To MS Access Database. Pin
Wendelius21-Feb-11 19:28
mentorWendelius21-Feb-11 19:28 
GeneralRe: ADO To MS Access Database. Pin
Mike Certini21-Feb-11 19:47
Mike Certini21-Feb-11 19:47 
GeneralRe: ADO To MS Access Database. Pin
Wendelius23-Feb-11 19:09
mentorWendelius23-Feb-11 19:09 
QuestionRetrieve N Record. Pin
Nanda_MR20-Feb-11 18:26
Nanda_MR20-Feb-11 18:26 
AnswerRe: Retrieve N Record. Pin
Mycroft Holmes20-Feb-11 18:59
professionalMycroft Holmes20-Feb-11 18:59 
GeneralRe: Retrieve N Record. Pin
Nanda_MR20-Feb-11 19:26
Nanda_MR20-Feb-11 19:26 
GeneralRe: Retrieve N Record. Pin
Mycroft Holmes20-Feb-11 20:20
professionalMycroft Holmes20-Feb-11 20:20 
GeneralRe: Retrieve N Record. Pin
Bernhard Hiller20-Feb-11 21:16
Bernhard Hiller20-Feb-11 21:16 
GeneralRe: Retrieve N Record. Pin
Nanda_MR20-Feb-11 22:36
Nanda_MR20-Feb-11 22:36 
GeneralRe: Retrieve N Record. Pin
Luc Pattyn21-Feb-11 2:09
sitebuilderLuc Pattyn21-Feb-11 2:09 
GeneralRe: Retrieve N Record. Pin
Mycroft Holmes21-Feb-11 11:36
professionalMycroft Holmes21-Feb-11 11:36 
GeneralRe: Retrieve N Record. Pin
Luc Pattyn21-Feb-11 11:40
sitebuilderLuc Pattyn21-Feb-11 11:40 
AnswerRe: Retrieve N Record. [modified] Pin
RyanEK20-Feb-11 19:15
RyanEK20-Feb-11 19:15 
GeneralRe: Retrieve N Record. Pin
Nanda_MR20-Feb-11 19:39
Nanda_MR20-Feb-11 19:39 
AnswerRe: Retrieve N Record. Pin
Jörgen Andersson20-Feb-11 19:48
professionalJörgen Andersson20-Feb-11 19:48 
GeneralRe: Retrieve N Record. Pin
Nanda_MR20-Feb-11 19:55
Nanda_MR20-Feb-11 19:55 
GeneralRe: Retrieve N Record. Pin
Jörgen Andersson20-Feb-11 20:30
professionalJörgen Andersson20-Feb-11 20:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.