Connect to SQL Server in C++ Application.
Managed C++/CLI
1
Posts
1
Posters
3
Views
1
Watching
-
Hi I created a C++ application from which i tried to connect to SQL Server DB. Initially i am not aware of this connection in C++. I know how to do it in .net. So, after googling i found one link in code project which explains how to connect to SQL Server in C++ application as mentioned in below link. [^] But in the above link, i dint find the sql connection string format. They have provided "Test". And i am not aware of DSN. I need a sample code of creating connection to my sql server in c++. I tried as follows but dint work for me.
int main()
{HENV hEnv = NULL; // Env Handle from SQLAllocEnv() HDBC hDBC = NULL; // Connection handle HSTMT hStmt = NULL;// Statement handle UCHAR szDSN\[MAX\_PATH\] = "DRIVER = {SQL Server} ;SERVER = in-spcdbsql1; DATABASE = SPC2017SQL\_Data; UID = sa; PWD = Password.1; Trusted\_Connection = True;";// Data Source Name buffer UCHAR szUID\[MAX\_PATH\] = "sa";// User ID buffer UCHAR szPasswd\[MAX\_PATH\] = "Intergraph.1";// Password buffer UCHAR szModel\[MAX\_PATH\];// Model buffer SDWORD cbModel;// Model buffer bytes recieved //char buff\[9\] = "Testing"; //UCHAR szSqlStr\[128\] = "INSERT into (Tablename) (ColumnName) Values ('Testing')"; RETCODE retcode; //sprintf((char\*)szSqlStr,"INSERT into (Tablename)(Columname) Values ('%s')",buff); // Allocate memory for ODBC Environment handle SQLAllocEnv(&hEnv); // Allocate memory for the connection handle SQLAllocConnect(hEnv, &hDBC); // Connect to the data source "test" using userid and password. retcode = SQLConnect(hDBC, szDSN, SQL\_NTS, szUID, SQL\_NTS, szPasswd, SQL\_NTS); if (retcode == SQL\_SUCCESS || retcode == SQL\_SUCCESS\_WITH\_INFO) { // Allocate memory for the statement handle retcode = SQLAllocStmt(hDBC, &hStmt); // Prepare the SQL statement by assigning it to the statement handle retcode = SQLPrepare(hStmt, szSqlStr, sizeof(szSqlStr)); // Execute the SQL statement handle retcode = SQLExecute(hStmt); // Project only column 1 which is the models SQLBindCol(hStmt, 1, SQL\_C\_CHAR, szModel, sizeof(szModel), &cbModel); // Get row of data from the result set defined above in the statement retcode = SQLFetch(hStmt); // Free the allocated statement handle SQLFreeStmt(hStmt, SQL\_DROP); // Disconnect from datasource SQLDisconnect(h