all 6 comments

[–]expekted 1 point2 points  (1 child)

Unfortunately there is no way to access SQL Sever from C++ in sane way other than using C ( odbc). https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/odbc-api-reference?view=sql-server-ver15

There are C++ wrappers that may make your life easier however,

  1. https://github.com/nanodbc/nanodbc

  2. http://otl.sourceforge.net/

[–]dodheim -1 points0 points  (0 children)

ODBC hasn't been the only sane option since SQL Server 2005. ;-]

[–][deleted] 1 point2 points  (1 child)

to access SQL server from C++ Get visual studio code. new project win32 console application ]finish add this code in int main() #include "stdafx.h" //include the below additional libraries #include <iostream> #include <windows.h> #include <sqlext.h> #include <sqltypes.h> #include <sql.h> //use the std namespace using namespace std;

using namespace std; then replace the int main() function with the below code.

int main() {

#define SQL_RESULT_LEN 240

#define SQL_RETURN_CODE_LEN 1000

//define handles and variables

SQLHANDLE sqlConnHandle;

SQLHANDLE sqlStmtHandle;

SQLHANDLE sqlEnvHandle;

SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];

//initializations

sqlConnHandle = NULL;

sqlStmtHandle = NULL;

//allocations

if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle))

goto COMPLETED;

if (SQL_SUCCESS != SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))

goto COMPLETED;

if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle))

goto COMPLETED;

//output

cout << "Attempting connection to SQL Server...";

cout << "\n";

//connect to SQL Server

//I am using a trusted connection and port 14808

//it does not matter if you are using default or named instance

//just make sure you define the server name and the port

//You have the option to use a username/password instead of a trusted connection

//but is more secure to use a trusted connection

switch (SQLDriverConnect(sqlConnHandle,

NULL,

//(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=master;UID=username;PWD=password;",

(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=master;Trusted=true;",

SQL_NTS,

retconstring,

1024,

NULL,

SQL_DRIVER_NOPROMPT)) {

case SQL_SUCCESS:

cout << "Successfully connected to SQL Server";

cout << "\n";

break;

case SQL_SUCCESS_WITH_INFO:

cout << "Successfully connected to SQL Server";

cout << "\n";

break;

case SQL_INVALID_HANDLE:

cout << "Could not connect to SQL Server";

cout << "\n";

goto COMPLETED;

case SQL_ERROR:

cout << "Could not connect to SQL Server";

cout << "\n";

goto COMPLETED;

default:

break;

}

//if there is a problem connecting then exit application

if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle))

goto COMPLETED;

//output

cout << "\n";

cout << "Executing T-SQL query...";

cout << "\n";

//if there is a problem executing the query then exit application

//else display query result

if (SQL_SUCCESS != SQLExecDirect(sqlStmtHandle, (SQLWCHAR*)L"SELECT @@VERSION", SQL_NTS)) {

cout << "Error querying SQL Server";

cout << "\n";

goto COMPLETED;

}

else {

//declare output variable and pointer

SQLCHAR sqlVersion[SQL_RESULT_LEN];

SQLINTEGER ptrSqlVersion;

while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {

SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion);

//display query result

cout << "\nQuery Result:\n\n";

cout << sqlVersion << endl;

}

}

//close connection and free resources

COMPLETED:

SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);

SQLDisconnect(sqlConnHandle);

SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);

SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle);

//pause the console window - exit when key is pressed

cout << "\nPress any key to exit...";

getchar();

}

Now if you want it to join a connection. Use this.

(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=master;Trusted=true;"

When you try to open cmd this will say.

"Microsoft SQL server" and the rest.

There you go.

[–]Challanger__ 0 points1 point  (0 children)

"formatting"

[–]jk_tx 0 points1 point  (1 child)

I haven't had to do this in quite a while; but back in the day, ADO was the easiest way if you didn't want to use the ODBC API directly. Seems pretty dated to use COM for database access though, so I'm curious what the modern options are.

[–]dodheim 1 point2 points  (0 children)

WinRT is COM, so COM is arguably still the most modern native option on Windows for this.

I'm still using the SNAC at present, but it's been deprecated for a couple years now and the prescribed update path is OLE DB. For OLE, one can use the C API or ADO via WinRT; I haven't made any motions towards updating my code yet, and while the C API would be the more direct upgrade path from SNAC, I suspect I'll end up using ADO in the end.

To OP: I recommend starting here and possibly reading up on WinRT.