【问题标题】:trying to connect to MSSQL through a C++ ODBC connection but cant find the right demo尝试通过 C++ ODBC 连接连接到 MSSQL,但找不到正确的演示
【发布时间】:2012-11-05 19:18:11
【问题描述】:

我正在使用我的 macbook pro 进行编程,需要连接到我公司的 MSSQL 服务器以对商业产品进行编程。

您实际上是如何连接到它的?我在看 MSDN 网站,但我不太明白。

出于演示的目的,我只是要在 XCODE 中创建一个新项目,然后创建一个将输出数据的控制台应用程序。一旦建立起来,就不能通过连接实现不同的东西。

编辑:添加了一些代码:

#include <iostream>
//#include <windows.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <sql.h>

using namespace std;

int main(int argc, const char * argv[])
{
SQLHENV hEnv;
SQLHDBC hDbc;
string connection = "AAA";
string db = "DB";
string user = "user";
string pass = "password";
string data = "DRIVER={SQL Server Native Client 11.0};SERVER="+connection+";DATABASE="+db+";UID="+user+";PWD="+pass+";";
//SQLCHAR* pwszConnStr = (SQLCHAR*)("Driver={SQL Server Native Client 11.0};Server="+connection+";Database="+db+";Uid="+user+";Pwd="+pass+";");
SQLCHAR* pwszConnStr = (SQLCHAR*)data.c_str();
//cout  << data << endl;
cout  << pwszConnStr << endl;
//error seems to occur in 1 of the 3 SQL statements below.
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);

RETCODE rc = SQLDriverConnect(hDbc, NULL, pwszConnStr, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_PROMPT);

if(rc == SQL_SUCCESS){
    cout << "Connected to the Database" << endl;
}else{
    cout << "No Connection Established" << endl;
}


return 0;
}

它无法编译,我认为这与我注释掉 windows.h 文件有关。问题是在我的 macbook pro 上找不到 windows.h,并且在 VStudios 中开发时发现它。

【问题讨论】:

    标签: c++ sql-server database odbc


    【解决方案1】:

    我推荐使用 c++ 使用 QT 库http://qt-project.org/downloads

    ...
    #include <QtSql>
    ...
    
    int main(int argc, char *argv[])
    {
        ....
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
        db.setDatabaseName("DRIVER={SQL Server};Server=YOUR SERVER;Database=YOUR DB NAME;User ID=YOUR USER;Password=YOUR PASS;Trusted_Connection=yes;");
    
        if(!db.open())
        {
           qDebug() << db.lastError().text();
    
           return 0;
        }
    
        ....
    }
    

    【讨论】:

    • aww snap,我忘了 QT。让我看看它是否足以满足我正在做的事情。
    • 那么您只需查询#include &lt;QSqlQuery&gt;{ QSqlQuery query; query.prepare("SELECT * FROM ... WHERE ..."); query.exec(); while(query.next()) { int test = query.value(0).toInt(); } query.finish(); }
    • 有没有比使用 QT 更好的方法。我知道我的一个萌芽主要将它用于 GUI,但我不确定它是否是我想做的好工具。
    • stackoverflow.com/questions/9336282/… 可能你不应该使用 windows.h 函数或用 posix 替换它
    【解决方案2】:

    一旦您离开 Windows,Microsoft 的文档的用途就会受到限制。他们对 ODBC API 的讨论很好——但任何关于 Visual Studio 或其他 Windows 特定组件的内容通常都应该被忽略。

    iODBC.org 可以提供一些指示 - 特别是如果您查看 iODBC Demo.app 和/或 iODBC Test.command 的源代码,它们与 free and open source iODBC SDK 一起提供。

    您还可以从使用商业支持的 ODBC 驱动程序(例如 my employer's offering)进行开发和测试中受益。

    【讨论】:

      【解决方案3】:

      在 stdafx.h 中:

      #pragma once
      
      #include <windows.h> //!first include
      #include <sqltypes.h> //!
      
      #include "targetver.h"
      #include <stdio.h>
      #include <tchar.h>
      

      在你的MainFile.cpp中:

       #include "stdafx.h"
       #include <iostream>
       #include <sql.h>
       #include <sqlext.h>
      
       using namespace std;
      
       int _tmain(int argc, _TCHAR* argv[])
       {
          RETCODE rc;        // ODBC return code 
          HENV henv;         // Environment 
          HDBC hdbc;         // Connection handle 
          HSTMT hstmt;       // Statement handle 
          SDWORD cbData;     // Output length of data
      
          // attempt to connect to the ODBC database 
          char db[] = "MSSQLSERVER"; // Server name 
          cout << "Attempting to open database " << db << "..." << endl; 
          SQLAllocEnv(&henv); 
          SQLAllocConnect(henv, &hdbc); 
          rc = SQLConnect(hdbc, (unsigned char*)db, SQL_NTS, 0, 0, 0, 0);
          cout << rc << endl;
      
          // if not successful, quit 
          if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) 
          { 
              cout << "Cannot open database -- make sure ODBC is configured properly."      << endl; 
              SQLFreeConnect(hdbc); 
              SQLFreeEnv(henv); 
              cout << "Press ENTER to continue." << endl; 
              cin.get(); 
              return 1; 
          }
          cout << "Hello, SQL-Server!" << endl;
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-08
        • 1970-01-01
        • 2013-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-30
        • 2017-08-16
        相关资源
        最近更新 更多