无法通过 ODBC 连接到Microsoft Azure 数据库
cannot connect to Microsoft Azure database through odbc
我的odbc使用以下代码(从这里开始:http://www.dreamincode.net/forums/topic/127959-odbc-c-example/(:
ODBC.cpp:
ODBC_Class::ODBC_Class()
{
ODBC_Class Example;
// Initialize The Return Code Variable
rc = SQL_SUCCESS;
// Allocate An Environment Handle
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &EnvHandle);
// Set The ODBC Application Version To 3.x
if (rc == SQL_SUCCESS)
rc = SQLSetEnvAttr(EnvHandle, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_UINTEGER);
// Allocate A Connection Handle
if (rc == SQL_SUCCESS)
rc = SQLAllocHandle(SQL_HANDLE_DBC, EnvHandle, &ConHandle);
}
// Define The Class Destructor
ODBC_Class::~ODBC_Class()
{
// Free The Connection Handle
if (ConHandle != NULL)
SQLFreeHandle(SQL_HANDLE_DBC, ConHandle);
// Free The Environment Handle
if (EnvHandle != NULL)
SQLFreeHandle(SQL_HANDLE_ENV, EnvHandle);
}
// Get the data for one column and return the info in the form
// of a std::string. The ODBC driver will make all necessary
// data conversions from whatever type the data is in the database
// to SQL_CHAR. You could make this function more comples by
// getting the return type as it appears in the database then constructing
// a VARIANT object to hold the data.
SQLRETURN ODBC_Class::GetColData(int colnum, string& str)
{
SQLCHAR buf[255] = { 0 };
if ((rc = SQLGetData(StmtHandle, colnum, SQL_CHAR, buf, sizeof(buf), NULL)) == SQL_SUCCESS)
str = reinterpret_cast<char*>(buf);
return rc;
}
//
// Define The ShowResults() Member Function
SQLRETURN ODBC_Class::GetResultset()
{
// Get all column description
DescribeColumns();
// erase anything that's in the colData vector
colData.clear();
// fetch a row from the resultset
while (SQLFetch(StmtHandle) == SQL_SUCCESS)
{
// vector of strings to hold the column data
vector<string> col;
string data;
// column counter
int i = 1;
// get the data for each column and add it to
// the col vector
while (GetColData(i, data) == SQL_SUCCESS)
{
col.push_back(data);
++i; // increment the column number
}
// add column data to the colData vector
colData.push_back(col);
}
return SQL_SUCCESS;
}
// Get the description for one column in the resultset.
// This was made a seprate function to simplify the coding
SQLRETURN ODBC_Class::Describe(ColDescription& c)
{
return SQLDescribeCol(StmtHandle, c.colNumber,
c.colName, sizeof(c.colName), &c.nameLen,
&c.dataType, &c.colSize, &c.decimalDigits, &c.nullable);
}
// Get the description for all the columns in the resultset.
void ODBC_Class::DescribeColumns()
{
ColDescription c;
c.colNumber = 1;
cols.clear();
while (Describe(c) == SQL_SUCCESS)
{
cols.push_back(c);
++c.colNumber;
}
}
在我的主文件中,我使用此代码执行它(它又是上面网站上的相同代码示例(
void Game::sqlConnection(const char *select) {
SQLCHAR DBName[] = "Driver=SQL Server Native Client 11.0;Server=tcp:myserver.ctp.database.windows.net;Database=PandemicMain;Uid=concordia;Pwd=University4;";
SQLCHAR SQLStmt[255] = { 0 };
SQLRETURN rc = SQL_SUCCESS;
if (Example.ConHandle != NULL)
{
rc = SQLConnect(Example.ConHandle, DBName, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
// Allocate An SQL Statement Handle
rc = SQLAllocHandle(SQL_HANDLE_STMT, Example.ConHandle, &Example.StmtHandle);
rc = SQLExecDirect(Example.StmtHandle, SQLStmt, SQL_NTS);
if (rc == SQL_SUCCESS)
{
// Define A SELECT SQL Statement
strcpy((char *)SQLStmt, select);
// Prepare And Execute The SQL Statement
rc = SQLExecDirect(Example.StmtHandle, SQLStmt, SQL_NTS);
// Display The Results Of The SQL Query
if (!rc == SQL_SUCCESS) {
cout << "*************************** failed ***************"
<< endl;
}
if (rc == SQL_SUCCESS)
{
Example.GetResultset();
// At this point you would want to do something
// with the resultset, such as display it.
}
}
// Free The SQL Statement Handle
if (Example.StmtHandle != NULL)
SQLFreeHandle(SQL_HANDLE_STMT, Example.StmtHandle);
// Disconnect From The Northwind Sample Database
rc = SQLDisconnect(Example.ConHandle);
}
}
我知道有这个网站:https://msdn.microsoft.com/en-us/magazine/dn630643.aspx 但我想知道是否可以使用此代码来做到这一点。我能够连接到我的本地数据库,但我需要我的队友能够从他们的房子运行代码,所以我在 Azure 上托管了我的数据库。
当使用给定的字符串运行上述代码时,我最终会得到来自 SqlConnect(( 的"-2"SQLRETURN。(仅发生在 Azure 数据库中,所以我假设我没有正确连接到它(
关于如何使此代码运行的任何见解还是徒劳的?
谢谢
另请记住,Azure SQL 具有基于 IP 地址的防火墙,该防火墙要求你将团队的家庭路由器列入白名单(可能会半频繁地更改(:
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-firewall-configure
相关文章:
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 带有 CKKS 的 Microsoft 密封中的矢量点积
- Azure Kinect 使用正文索引映射裁剪正文
- 如何添加预防措施以绕过未注册Microsoft.ACE.OLEDB.12.0?
- CPP 包括 Azure DevOps 中的目录设置
- Libmosquitto publish 不会将所有消息传递到 Azure IoT Hub
- 与其他编译器相比,相同的代码在工作室Microsoft提供不同的输出
- 使用 Outlook 2016 Microsoft Store 连接到 Outlook COM
- 如何在 SEAL 3.1 中使用 CRT 批处理技术Microsoft?
- 使用 CMake,Microsoft MPI 和 Visual Studio 2017 找不到 mpi.h
- 如何在视觉c ++项目中添加箭头键作为修饰键Microsoft
- 为什么Microsoft在新代码中使用结构而不是类?
- Microsoft C++ 用于图形 API 的 Rest SDK
- 在 Azure DevOps 构建管道中使用英特尔C++编译器为 Linux 环境构建C++代码
- Microsoft ODBC 无法创建有效的句柄
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- 使用IMFSourceReader(Microsoft媒体基金会)进行音频流传输
- Azure Storage CPP & VCPKG
- 无法通过 ODBC 连接到Microsoft Azure 数据库
- C++ in the cloud. Casablanca REST Service on Microsoft Azure