se connecter a une base depuis un programme en C++
bonjour:D
J'ai besoin d'avoir accèder à une base de donnée à partir d'un programme en C++
J'ai donc besoin des primitives connect, write, read ou get, commit, fetch...
ou trouver des exemples de cet ordre?
pour le connect la solution est là
bonjour
Il y a deux choses a faire pour connecter la base
Ajouter un assembly qui se trouve /Program Files/Microsoft SQL Server/100/SDK/Assemblies/Microsoft.SqlServer.Types.dll
mettre un code adapté:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
namespace SpecificationChecker
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
ref class CInterbaseManagerM
{
private:
static SQLHANDLE* sqlenvhandle;
static SQLHANDLE* sqlconnectionhandle;
static SQLHANDLE* sqlstatementhandle;
static SQLRETURN retcode;
public:
CInterbaseManagerM::CInterbaseManagerM();
CInterbaseManagerM::~CInterbaseManagerM();
static void CInterbaseManagerM::edit( char* final_err_buff);
static erc CInterbaseManagerM::ConnectServer(std::wstring wsUserName, std::wstring wsPassword, std::wstring wsHost);
static List<ElementSemanticM^>^ CInterbaseManagerM::ExecuteSelectRequestAndReadElement(std::wstring wsRequest, unsigned long* dwNbResultFound);
static List<ElementLinksM^>^ CInterbaseManagerM::ExecuteSelectAndLinkRequest( std::wstring wsRequest, ElementSemanticM^ pEnreg, unsigned long &dwNbResultFound);
static erc CInterbaseManagerM::GetWideString(short shIndexField, std::wstring & wsStringValue);
static List<ElementWork^>^ CInterbaseManagerM::ExecuteSelectRequestAndReadWork(std::wstring wsRequest, unsigned long* dwNbResultFound);
};
}; |
et la procédure
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
if( ErrorCode == INIT_NO_ERROR || ErrorCode == 1)
{
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, sqlenvhandle))
;
if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
;
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, sqlconnectionhandle))
;
SQLWCHAR retconstring[1024];
switch(SQLDriverConnect (sqlconnectionhandle, NULL, (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=COHERENCE;UID=sa;PWD=Admin-123;", SQL_NTS, retconstring, 1024, NULL,SQL_DRIVER_NOPROMPT))
{
case SQL_SUCCESS_WITH_INFO:
show_error(SQL_HANDLE_DBC, (const SQLHANDLE*)sqlconnectionhandle);
break;
case SQL_INVALID_HANDLE:
case SQL_ERROR:
show_error(SQL_HANDLE_DBC, (const SQLHANDLE*)sqlconnectionhandle);
retcode = -1;
break;
default:
break;
}
if(retcode == -1)
;
if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, sqlstatementhandle))
;
} |
ca compile alors :il reste a le vérifier à l'exécution.