2

我在我的 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 中开发时发现它是。

4

3 回答 3

4

使用 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;
    }

    ....
}
于 2012-11-05T19:50:13.623 回答
1

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

iODBC.org 可以提供一些指导——特别是如果您查看 iODBC Demo.app 和/或 iODBC Test.command 的源代码,它们随免费和开源的 iODBC SDK一起提供。

您还可以从使用商业支持的 ODBC 驱动程序(例如我的雇主提供的)进行开发和测试中受益。

于 2012-11-05T21:32:49.913 回答
1

在 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;
}
于 2015-08-08T16:49:01.393 回答