简体   繁体   English

如何通过c++连接ODBC DSN

[英]How to connect to ODBC DSN through c++

I made a PostgreSQL30 data source name.我做了一个PostgreSQL30的数据源名称。 I tested it and I managed to connect to that dsn through ODBC data source window manager.我对其进行了测试,并设法通过 ODBC 数据源 window 管理器连接到该 dsn。 There was a button Test and It showed message dialog, telling me that connection was success.有一个按钮测试,它显示消息对话框,告诉我连接成功。

Now I wonder how would I connect to that DSN through c++ code and get some data.现在我想知道如何通过 c++ 代码连接到该 DSN 并获取一些数据。

This is my code, i saw few examplex online and come up with this code这是我的代码,我在网上看到了几个 examplex 并想出了这段代码

enter code here

HENV hEnv = NULL; // for allocating memory usingSQLAllocEnv
HDBC hDBC = NULL; // connection handler
HSTMT hStmt = NULL; // statement handler
const char* szDSN = "PostgreSQL30"; // DataSourceName (config in windows 
control panel)
const char* szUID = "postgres"; //username of the database
const char* szPasswd = "postgres"; //password of the database

RETCODE retcode;
int rcod = 1;
int i, j, no = 2;



int main()
{ 
  retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);

  std::cout << retcode << std::endl;

  SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

  retcode = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDBC);

  std::cout << retcode << std::endl;

   retcode = SQLConnectA(hDBC,(SQLCHAR*)szDSN, SQL_NTS, (SQLCHAR*)szUID, SQL_NTS, 
  (SQLCHAR*)szPasswd, 
  SQL_NTS);


std::cout << (SQLCHAR*)szUID << std::endl;


if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
    // connected!!
    std::cout << "Hello World!\n SUCCESSS COME ON PLEASE";
}
else {
    std::cout << retcode << std::endl;
}


SQLFreeHandle(SQL_HANDLE_ENV, &hEnv);
SQLFreeHandle(SQL_HANDLE_DBC, &hDBC);

} }

But I cant comprehend why would this work, I mean how is this code knows to call SQLAllocHandle implemented by Postgres driver?但我无法理解为什么会这样,我的意思是这段代码如何知道调用由 Postgres 驱动程序实现的 SQLAllocHandle?

My question is how would I connect to DSN thorugh C++ code and get some data我的问题是如何通过 C++ 代码连接到 DSN 并获取一些数据

In fact ODBC Driver Manager makes these calls to ODBC driver when performing your SQLConnect call: on this stage it "knows" which driver you want to use.事实上,在执行 SQLConnect 调用时,ODBC 驱动程序管理器会对 ODBC 驱动程序进行这些调用:在此阶段,它“知道”您要使用哪个驱动程序。 Look at the SQLConnect function description, "Comments" section:查看SQLConnect函数说明,“注释”部分:

"The Driver Manager does not connect to a driver until the application calls a function (SQLConnect, SQLDriverConnect, or SQLBrowseConnect) to connect to the driver. Until that point, the Driver Manager works with its own handles and manages connection information. When the application calls a connection function, the Driver Manager checks whether a driver is currently connected to for the specified ConnectionHandle: “在应用程序调用函数(SQLConnect、SQLDriverConnect 或 SQLBrowseConnect)连接到驱动程序之前,驱动程序管理器不会连接到驱动程序。在那之前,驱动程序管理器使用自己的句柄并管理连接信息。当应用程序调用连接函数,驱动程序管理器检查驱动程序当前是否连接到指定的 ConnectionHandle:

  • If a driver is not connected to, the Driver Manager connects to the driver and calls SQLAllocHandle with a HandleType of SQL_HANDLE_ENV, SQLAllocHandle with a HandleType of SQL_HANDLE_DBC, SQLSetConnectAttr (if the application specified any connection attributes), and the connection function in the driver.如果未连接到驱动程序,驱动程序管理器将连接到驱动程序并调用 HandleType 为 SQL_HANDLE_ENV 的 SQLAllocHandle、句柄类型为 SQL_HANDLE_DBC 的 SQLAllocHandle、SQLSetConnectAttr(如果应用程序指定了任何连接属性)以及驱动程序中的连接函数。 The Driver Manager returns SQLSTATE IM006 (Driver's SQLSetConnectOption failed) and SQL_SUCCESS_WITH_INFO for the connection function if the driver returned an error for SQLSetConnectAttr.如果驱动程序返回 SQLSetConnectAttr 错误,驱动程序管理器将返回 SQLSTATE IM006(驱动程序的 SQLSetConnectOption 失败)和 SQL_SUCCESS_WITH_INFO 用于连接函数。 For more information, see Connecting to a Data Source or Driver.有关更多信息,请参阅连接到数据源或驱动程序。

  • If the specified driver is already connected to on the ConnectionHandle, the Driver Manager calls only the connection function in the driver.如果指定的驱动程序已经连接到 ConnectionHandle 上,则驱动程序管理器仅调用驱动程序中的连接函数。 In this case, the driver must make sure that all connection attributes for the ConnectionHandle maintain their current settings.在这种情况下,驱动程序必须确保 ConnectionHandle 的所有连接属性都保持其当前设置。

  • If a different driver is connected to, the Driver Manager calls SQLFreeHandle with a HandleType of SQL_HANDLE_DBC, and then, if no other driver is connected to in that environment, it calls SQLFreeHandle with a HandleType of SQL_HANDLE_ENV in the connected driver and then disconnects that driver.如果连接到不同的驱动程序,驱动程序管理器调用 SQLFreeHandle 的 HandleType 为 SQL_HANDLE_DBC,然后,如果在该环境中没有连接到其他驱动程序,则它调用连接的驱动程序中的 HandleType 为 SQL_HANDLE_ENV 的 SQLFreeHandle,然后断开该驱动程序的连接. It then performs the same operations as when a driver is not connected to.然后它执行与驱动程序未连接时相同的操作。

The driver then allocates handles and initializes itself."然后驱动程序分配句柄并初始化自己。”

Full text here: SQLConnect function comments全文在这里: SQLConnect 函数注释

const char* szDSN = "PostgreSQL30"; // DataSourceName (config in windows 
control panel)

If you check in the ODBC Data source, This DSN (PostgreSQL30) would be created using an appropriate driver.如果您签入 ODBC 数据源,将使用适当的驱动程序创建此 DSN (PostgreSQL30)。 The driver manager knows which driver to load based on the configured ODBC data source information.驱动管理器根据配置的ODBC数据源信息知道加载哪个驱动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM