0

I´m having a problem dealing with the connection to sql server in c++. Actually i don t know where the problem come from. I took a look at my firewall open the port 1433/1434 - on sql configuration manager i also have enable for the remote connexion. It returns the error message:

SQL DRiver message:[Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. SQLState08001.

Could you help me to solve this problem?

My code looks like this:

#pragma once
using namespace std;
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <list>
#include <sqlext.h> // main SQLAPI++ header
#include <sqltypes.h>
#include <Windows.h>
#include <sql.h>
#include <cstring>
#include <string>

void showSQLError(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 << "SQL DRiver message:" << message << "\nSQLState" << SQLState << "." << endl;
}

class Datapoint {
public:
    time_t Date; string Name; double Value;



    Datapoint() {
        //default constructor to initialize Datapoint
        ;
    }

    Datapoint(time_t Dates, string Names, double Values)
    {
        Date = Dates;
        Name = Names;
        Value = Values;
    }

    static list<Datapoint> Timeseries(string Names)
    {
        list<Datapoint> data;

        SQLHANDLE SQLEnvHandle = NULL;
        SQLHANDLE SQLConnexionHandle = NULL;
        SQLHANDLE SQLStatementHandle = NULL;
        SQLRETURN retcSQLQuerystringode = 0;
        int retcode;
        // assigning value to string SQLQuerystring
        string SQLQuerystring = "SELECT * FROM MDA..MarketData WHERE Name = '" + Names + "' order by Date Asc";

        // declaring character array (+1 for null terminator)
        char* SQLQuery = new char[SQLQuerystring.length() + 1];

        // copying the contents of the string to char array
        strcpy(SQLQuery, SQLQuerystring.c_str());

        do {
            // Allocate an environment handle
            if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &SQLEnvHandle))
            {
                break;
            }

            // Set the ODBC version environment attribute
            if (SQL_SUCCESS != SQLSetEnvAttr(SQLEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
            {
                break;
            }
            // Allocate a connection handle
            if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, SQLEnvHandle, &SQLConnexionHandle))
            {
                break;
            }
            if (SQL_SUCCESS != SQLSetConnectAttr(SQLConnexionHandle, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0))
            {
                break;
            }

            SQLCHAR retConnString[1024];
            switch (SQLDriverConnect(SQLConnexionHandle, NULL, (SQLCHAR*)"Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-FT3KNQR\MSSQLSERVER01;Database=MDA;UID=DESKTOP-FT3KNQR\mathi;", SQL_NTS, retConnString, 1024, NULL, SQL_DRIVER_NOPROMPT)) {
            case SQL_SUCCESS:
                break;
            case SQL_SUCCESS_WITH_INFO:
                break;
            case SQL_NO_DATA_FOUND:
                showSQLError(SQL_HANDLE_DBC, SQLConnexionHandle);
                retcode = 1;
                break;
            case SQL_INVALID_HANDLE:
                showSQLError(SQL_HANDLE_DBC, SQLConnexionHandle);
                retcode = 1;
                break;
            case SQL_ERROR:
                showSQLError(SQL_HANDLE_DBC, SQLConnexionHandle);
                retcode = 1;
                break;
            default:
                break;
            }

            if (retcode == -1)
            {
                break;
            }

            if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, SQLConnexionHandle, &SQLStatementHandle))
            {
                break;
            }

            if (SQL_SUCCESS != SQLExecDirect(SQLStatementHandle, (SQLCHAR*)SQLQuery, SQL_NTS)) 
            {
                showSQLError(SQL_HANDLE_STMT, SQLStatementHandle);
                break;
            }
            else
            {

                while (SQLFetch(SQLStatementHandle) == SQL_SUCCESS)
                {

                    time_t datefromdb;
                    float valuesfromdb;
                    char* namesfromdb = new char[256];
                    SQLGetData(SQLStatementHandle, 1, SQL_C_DEFAULT, &datefromdb, sizeof(datefromdb), NULL);
                    SQLGetData(SQLStatementHandle, 2, SQL_C_DEFAULT, &namesfromdb, sizeof(namesfromdb), NULL);
                    SQLGetData(SQLStatementHandle, 3, SQL_C_DEFAULT, &valuesfromdb, sizeof(valuesfromdb), NULL);

                    Datapoint datapointsql;
                    datapointsql.Date = datefromdb;
                    datapointsql.Name = namesfromdb;
                    datapointsql.Value = valuesfromdb;
                    data.push_back(datapointsql);
                }
            }

        } while (FALSE);

        // Free the statement handle
        SQLFreeHandle(SQL_HANDLE_STMT, SQLStatementHandle);

        // Disconnect from the SQL Server
        SQLDisconnect(SQLStatementHandle);

        // Free the connection and environment handles
        SQLFreeHandle(SQL_HANDLE_DBC, SQLConnexionHandle);
        SQLFreeHandle(SQL_HANDLE_ENV, SQLEnvHandle);

        return data;
    }
};

using namespace std;
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <list>
#include <sqlext.h> // main SQLAPI++ header
#include <sqltypes.h>
#include <Windows.h>
#include <sql.h>
#include <cstring>
#include <string>
#include "Class_Datapoint.h"

int main()
{
    list<Datapoint> data = Datapoint::Timeseries("CCC");

    return 0;
}

I took a look at my firewall open the port 1433/1434 - on sql configuration manager i also have enable for the remote connexion. The connexion with the database doesn t work.I am using a windows authentification without password. Server name:DESKTOP-FT3KNQR\MSSQLSERVER01 / User name:DESKTOP-FT3KNQR\mathi/ without password.

3
  • As a first troubleshooting step, run this from a command prompt to verify port connectivity: powershell tnc "DESKTOP-FT3KNQR" -Port 1433
    – Dan Guzman
    Feb 5 at 15:11
  • Hi Dan, Thx for your answer, the tcp test was successfull.
    – mathmath
    Feb 5 at 16:29
  • Since you're using the default port, specify Server=DESKTOP-FT3KNQR. The instance name isn't needed and introduces a dependency on the SQL Server Browser.
    – Dan Guzman
    Feb 5 at 16:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.