试图通过C ODBC连接连接到MSSQL,但找不到正确的演示

trying to connect to MSSQL through a C++ ODBC connection but cant find the right demo

本文关键字:连接 找不到 MSSQL ODBC      更新时间:2023-10-16

我正在MacBook Pro上编程,需要连接到公司的MSSQL Server来编程商业产品。

您如何真正连接到它?我在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文件有关。问题是Windows.h在我的MacBook Pro上找不到,并且在Vstudios中开发时都没有发现。

with c i reccomend使用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;
    }
    ....
}

Microsoft的文档在您离开Windows后的使用率有限。他们对ODBC API的讨论很好 - 但是有关Visual Studio或其他特定于Windows的组件的任何内容通常都应忽略。

iodbc.org可以提供一些指针 - 尤其是如果您查看iodbc demo.app和/或iodbc test.command的源,它带有免费和开源的iodbc sdk。

您也可以通过商业支持的ODBC驱动程序来开发和测试,例如我的雇主的产品。

in stdafx.h:

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

在yourmainfile.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;
}