Hy,
Working actually on a dll in order to learn how to use sql server i found a good tutorial about this subject
http://www.codersource.net/c++_ado_stored_procedure.html.
Based on this tutorial i tried to do my own dll, here s my code
-----------------------------------------------------------------------
#include <afxwin.h> //This program uses MFC
#include <Afxdisp.h>
#include <stdio.h>
#import "C:\Program Files\Fichiers communs\System\ado\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
int main()
{
/*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;
_CommandPtr pCom;
// Define string variables for connection
_bstr_t strCon("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=user;Password=password;Initial Catalog=dtatbasename;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\n");
goto cleanup;
}
//Create the C++ ADO Command Object
pCom.CreateInstance(__uuidof(Command));
pCom->ActiveConnection = pConn;
//Make the ADO C++ command object to accept stored procedure
pCom->CommandType = adCmdStoredProc ;
//Tell the name of the Stored Procedure to the command object
pCom->CommandText = _bstr_t("dbo.spTestTable");
//Prepare the Name VARIANT for ADO C++ Command Object Parameter
VARIANT vName;
vName.vt = VT_BSTR; //Variant type for BSTR
vName.bstrVal = _bstr_t("CoderSource C++ ADO Stored Procedure Sample");
//Prepare the Age VARIANT for ADO C++ Command Object Parameter
VARIANT vAge;
vAge.vt = VT_I2; //Variant type for Integer
vAge.intVal = 10;
//Prepare the Salary VARIANT for ADO C++ Command Object Parameter
COleCurrency vOleSalary(5000,55);
//Use COleDateTime class for Date type
COleDateTime vOleDOB( 2004, 2,1 , 0, 0 , 0 ) ;
//Add Parameters to the C++ ADO Command Object
//This adds the string parameter
pCom->Parameters->Append(pCom->CreateParameter(_bstr_t("strCompanyName"),adChar,adParamInput,50,vName));
pCom->Parameters->Append(pCom->CreateParameter(_bstr_t("iAge"),adInteger,adParamInput,4,vAge));
pCom->Parameters->Append(pCom->CreateParameter(_bstr_t("dob"),adDate,adParamInput,8,_variant_t(vOleDOB)));
pCom->Parameters->Append(pCom->CreateParameter(_bstr_t("mSalary"),adCurrency,adParamInput,8,_variant_t(vOleSalary)));
_variant_t vNull;
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
pCom->Execute(NULL,NULL,adCmdStoredProc);
printf("Data Added Successfully\n");
//Close the database
pConn->Close();
}
catch(_com_error & e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\n Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}
cleanup:
CoUninitialize();
return 0;
}
----------------------------------------------------------------
I created my table using a windows connection and so changed the username and password in the code above. When i try to compile this example i got 1 error and 1 warning !!
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
StdAfx.cpp
c:\program files\microsoft visual studio\myprojects\test\debug\msado15.tlh(407) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
c:\program files\microsoft visual studio\vc98\include\comip.h(46) : error C2857: '#include' statement specified with the /Ycstdafx.h command-line option was not found in the source file
Error executing cl.exe.
StdAfx.obj - 1 error(s), 1 warning(s)
----------------------------------------------------------------------------
Could someone please help me fixing the problem ?
I would like to add that i am using Visual C++ 6.0. The original author warns that we have to link with the MFC Library in the Visual Studio properties dialog but i don't know how to ?!
Thanks in advance,