SQL SERVER

Mar 20, 2009 at 7:53pm

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,


Mar 21, 2009 at 7:06am
I am not sure abt the error but u can check a couple of things.

check which type of project you have created. it should be an mfc project.
secondly check the settings of precompiled header in C++ options.
because the error is realted to stdafx.h which is the default pre-compiled header included by vc++ 6.0.
if you dont have any file stdafx.h then use the option:
not using precompiled header.

you dont have to link with mfc libraries because when you create a mfc project it will automatically select the .lib files.
Mar 21, 2009 at 8:09pm
Thanks for your elp but i still need your help. You are asking me to create a mfc project but when i use visual studio 6.0 i got only three choices mfc ActiveX ControlWizard, MFC Appwizard ( dll ) and finally MFC Appwizard (exe). My purpose is to create a dll so iused the second template MFC Appwizard (dll) am I wrong ?
Mar 22, 2009 at 6:19am
thats corrrect.. this is the only way to create a mfc dll. then you are creating a correct dll.
but your problem is that it is not compiling.. correct?
though i have not seen this error before but looking at it, it seems that is is realated to precompiled header stdafx.h!!

if you can tell me the steps you used to create your dll and can give the least possible code which is giving error then i could make a project here and can tell you exactly whats wrong.


Mar 22, 2009 at 6:20am
your error is coming during the compilation step so its not related to if you are linking with correct library or not. you are not reaching at linking stage so dont worry about the correct or incorrect libraries.

Mar 22, 2009 at 10:05am
Hy

As a eplained i used visual Studio C++ 6.0 to make this dll. I created a new roject named test and used the MFC Appwizard template. I wrote the code on the test.cpp file and tried to compile it and got the error message :

--------------------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)

-----------------------------------------------------------------------------------

I will add andmay be that's the problem iwrote the code on the test.cpp file and not on the StdAfx.cpp ... and maybe this is the error .?!


Mar 22, 2009 at 10:10am
Sorry for the last message i make an error... I wrote the code on the StdAfx.cpp and not test.cpp !!! Sorry again and would like to add i didn't write anything else on any file nor changed any options/properties !
Mar 22, 2009 at 10:50am
Hy Ithink i found the error and tried whith a new project but changed the properties and asked to noy use precompiled headers !!! Here's the 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=sabri;Password=zanzibar;Initial Catalog=MT4TRADE;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;
}

Should i write this code on the test.cpp template or StdAfx.cpp ? I ued the test2.cpp template and compile it and i got anu error message !! Excellent but when i check my table in sql server i didn't found any records!! if you check my dll i am askng to open a table on sql server and write in this table some values for example vOleSalary(5000,55)... Any idea why it doesn't work ?

Thanks
Mar 22, 2009 at 12:40pm
if you have selected the option of dont use precompiled headers then delete stdafx.cpp and stdafx.h from your project.
write code in test.cpp and compile. it should compile with no errors. ok?
or when you create a mfc dll vc++ adds some files to your project automatically. so you can add your code to them also.
remember you dont need the main function because the dll dont have a main function. ok. so name it as something else and call if from your .exe file.

if still you have problems then tell me..i will tell you the details steps. right now cant tell as im using linux.. i've to boot into windows.
Mar 22, 2009 at 1:03pm
Thank you again for your help. I deleted stdafx.cpp and stdafx.h and compile it, NO ERORS but still two warnings

c:\program files\microsoft visual studio\myprojects\test2\debug\msado15.tlh(407) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
c:\program files\microsoft visual studio\myprojects\test2\test2.cpp(21) : warning C4129: 'S' : unrecognized character escape sequence

But i think it's not a problem. I created a database ans table on sql server and asked to use windows certification to connect at this database and server, so i didn't change any login or password. Do i have to write something on the code above to tell the dll to connect using windows certification ? Do i have to mention the name of my server which is actually ACER\SQLSERVER ?

Thanks
Topic archived. No new replies allowed.