我的ODBC使用下面的代碼(從這裏:http://www.dreamincode.net/forums/topic/127959-odbc-c-example/):無法通過ODBC
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,但我不知道是否有可能與此代碼做。我能夠連接到我的本地數據庫,但我需要我的隊友能夠從他們的房子運行代碼,所以我將我的數據庫託管在天藍色。
當使用給定的字符串運行上面的代碼時,我最終得到了來自SqlConnect()的「-2」SQLRETURN。 (只發生在天藍數據庫,所以我假設我沒有連接到它的權利)
任何有關如何使此代碼運行的見解或它是徒勞的?
謝謝
如果任何人使用上面的教程,並且對cpp(像我)是新手,並試圖理解爲什麼,SQLConnect需要用戶名和密碼。我應該看起來更深。 – Pepe