0

The original BJTIMES project had used the built-in Windows ADO framework for connecting to the DB. This limits the solution to run only on Windows. I am modifying the solution to use ODBC so that with the use of Windows/Linux ODBC drivers, we can run the solution on Windows as well as Linux

I tried to convert the ADO to ODBC but when I run the solution it says failed to connect. That means it is not connected to Database. But it works fines in ADO version though

try
{     //following ODBC functions to establish connection
    if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &SqlEnvHandle ))        

        if (SQL_SUCCESS == SQLSetEnvAttr(getEnvironment(), SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))

            if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, getEnvironment(), &SqlConnectionHandle ))
            {

                SQLCHAR retconstring[RETURN_CODE_SZ];

                SQLRETURN retcode = SQLDriverConnect(  //establishing the connection to DB
                    getConnection(),
                    NULL,
                    connect,
                    SQL_NTS,
                    retconstring,
                    1024,
                    NULL,
                    SQL_DRIVER_NOPROMPT);

                if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
                {
                    isConnected = true;
                    sprintf(m_ErrStr, "Success");
                    IsConnected = true;
                    return IsConnected;
                }
                else
                {
                    sprintf(m_ErrStr, "Failed to connect");
                    return false;
                }
            }

}

CATCHERROR(SqlConnectionHandle,0);

return false;
}

This is the part of code for the connection to the database.

In the above code is there variable that uses ADO rather than ODBC. It looks like SQLdriverconnect has some problem. How can I figure out the problem?

halfer
  • 18,701
  • 13
  • 79
  • 158
  • Read the __Diagnostics__ section in the help and add the missing code to find out why: https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqldriverconnect-function?view=sql-server-ver15 – Richard Critten Oct 23 '19 at 18:52
  • take a look at using ADO with Linux. See https://stackoverflow.com/questions/1172429/deploy-asp-net-mvc-on-linux-best-practices-tools-surprises as well as https://stackoverflow.com/questions/37738106/net-core-vs-mono Microsoft is doing much more Linux and open source work these days. – Richard Chambers Oct 23 '19 at 20:05
  • Richard Can you help me adding the missing code?? I tried many but still the same error. The problem seems to be in SQLdriverConnect. Sqldriverconnect return the value -1 but the value that I need is either 0 or 1 so that it can move to if statement but now due to retcode=-1 it directly moves to else and return Failed to connect string rather than going into if statement – Sumit Koirala Oct 23 '19 at 21:16

0 Answers0