Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Connect to SQL Server in C++ Application.

Connect to SQL Server in C++ Application.

Scheduled Pinned Locked Moved Managed C++/CLI
databasecsharpc++sql-servercom
1 Posts 1 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sampath579
    wrote on last edited by
    #1

    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
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups