Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sqlserver c++ connection?LOGIN FAILED

Author  Topic 

erpis
Starting Member

2 Posts

Posted - 2011-08-02 : 06:12:13
Sqlserver c++ connection?
// SqlDeneme3.cpp : main project file.

#include "stdafx.h"

#include <string>
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;
using namespace System;
using namespace std;



int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
String ^constr = "Provider=sqloledb;Server=a-d-3-pc\\mssq…

OleDbConnection ^myConnection = gcnew OleDbConnection("Provider=sqloledb;Server=a-d-3-PC\\mssqlserverdb Id=sa; Password=XXXXX;Trusted_Connection=Tru…
// myConnection->ConnectionString = "Provider=Microsoft.Jet.OLEBD.4.0; Data Source=C:\my_databases\taxLots.mdb";
try
{
myConnection->Open();
Console::Write("open");
if (myConnection->State == ConnectionState::Open){
Console::WriteLine("Connection opened successfully");
Console::Write("girildi_correct");
}
else{
Console::WriteLine("Connection could not be established");
Console::Write("girildi_false");
}
}
catch(Exception ^ex)
{
Console::Write(ex->Message);
}
__finally
{
myConnection->Close();
}



return 0;
}
/*my code is collapsed while creating dbconnection object therefore i cannot create a connection btw visual studio 2008 and sql server by using c++

For Answers
Thanks in Advance :D

erpis
Starting Member

2 Posts

Posted - 2011-08-02 : 06:36:16
EXTRA INFO: SQL SERVER 2008 is installed on my pc, i tried some codes to create connection however i encountered with some problems

One of the code i implemented was:

#include <iostream>
#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <sqlext.h>

using namespace std;

void show_error(unsigned int handletype, const SQLHANDLE& handle){
SQLCHAR sqlstate[1024];
SQLCHAR message[1024];
if(SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL))
cout<<"Message: "<<message<<"\nSQLSTATE: "<<sqlstate<<endl;
}

int main(){

SQLHANDLE sqlenvhandle = NULL;
SQLHANDLE sqlconnectionhandle = NULL;
SQLHANDLE sqlstatementhandle = NULL;
SQLRETURN retcode= NULL;

if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
goto FINISHED;

if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
goto FINISHED;

if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
goto FINISHED;

SQLCHAR retconstring[1024];

switch(SQLDriverConnect (sqlconnectionhandle,
NULL,
(SQLCHAR*)"DRIVER={SQL Server};SERVER=A-D-3-PC\\SQLEXPRESS;DATABASE=RTMSDB;UID=A-D-3-PC\\abilgi3;",
SQL_NTS,
retconstring,
1024,
NULL,
SQL_DRIVER_NOPROMPT)){
case SQL_SUCCESS_WITH_INFO:
show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
break;
case SQL_INVALID_HANDLE:
case SQL_ERROR:
show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
goto FINISHED;
default:
break;
}

if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))
goto FINISHED;

if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"select * from Company", SQL_NTS)){
show_error(SQL_HANDLE_STMT, sqlstatementhandle);
goto FINISHED;
}
else{
char text[64];
int id;
while(SQLFetch(sqlstatementhandle)==SQL_SUCCESS){
SQLGetData(sqlstatementhandle, 1, SQL_C_ULONG, &id, 0, NULL);
SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, text, 64, NULL);
cout<<id<<" "<<text<<endl;
}
}

FINISHED:
SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
SQLDisconnect(sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);

}
I installed sqlapi and adjusted all properties of project.
Go to Top of Page
   

- Advertisement -