试图通过C ODBC连接连接到MSSQL,但找不到正确的演示
trying to connect to MSSQL through a C++ ODBC connection but cant find the right demo
我正在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;
}
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- QObject::连接不起作用 - 使用函数语法找不到信号
- 菜单操作连接找不到插槽
- 在Eclipse中找不到用于连接MySQL的C++代码的库