I’ve found a good example on the MSDN site. SQLConnect
I want to connect to an SQL server, to my database, but unfortunately, SQLConnect always returns -1, and the application freezes.
I have edited the code above:
direxec::direxec()
{
_mbscpy_s( chr_ds_name, MAX_DATA, ( const unsigned char * )"Server=mySQLServer;Database=myDatabaseName;" );
_mbscpy_s( uid, MAX_DATA, ( const unsigned char * )"testuser" );
_mbscpy_s( pwd, MAX_DATA, ( const unsigned char * )"testpassword" );
printf("\n%s",chr_ds_name);
}
void direxec::sqlconn()
{
SQLAllocEnv( &henv );
SQLAllocConnect( henv, &hdbc );
rc = SQLConnect( hdbc, chr_ds_name, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS );
// Deallocate handles, display error message, and exit.
if ( !MYSQLSUCCESS( rc ) )
{
printf( "\nFailed %d",rc );
SQLFreeConnect( henv );
SQLFreeEnv( henv );
SQLFreeConnect( hdbc );
if ( hstmt ) error_out();
exit( -1 );
}
printf("\nConnected");
rc = SQLAllocStmt( hdbc, &hstmt );
}
Using SQLDriverConnect i can connect to my database, but i want to use SQLConnect if it is possible.
Does anyone have any idea what am i doing wrong?
Thanks!
SqlConnectdoesn’t accept a connection string, it only accepts a Data Source Name (DSN). DSN’s are configured in the ODBC Administration Tool in Administrative Tools. If you want to use a connection string then you need to useSqlDriverConnect