我正在创建一个 C++ 应用程序,它从文件中收集连接字符串和查询,然后连接到数据库并运行查询并打包结果,但我遇到了 Sage 300 的问题,但无论我如何处理连接字符串它不会连接。请看一下,如果我错过了什么请告诉我
Error: Error connecting to the database.
ConnectionString: DSN={CP TIMBERLINE};uid=<User>;pwd=<Password>;
Query: SELECT * from JCM_MASTER__JOB_1
这是我的代码:
std::ifstream file(filePath);
if (!file.is_open()) {
std::ofstream errorFile("Resultsfor_Error.csv", std::ios_base::app);
errorFile << "Error opening file: " << filePath << std::endl;
return;
}
std::cout << "processPriorityFile Started for " + filePath;
ThreadData data;
std::string line;
while (std::getline(file, line)) {
if (line.find("Thread_ID:") != std::string::npos) {
data.threadID = std::stoi(line.substr(line.find(":") + 1));
}
else if (line.find("Connectionstring:") != std::string::npos) {
data.connectionString = line.substr(line.find(":") + 1);
}
else if (line.find("Query:") != std::string::npos) {
data.query = line.substr(line.find(":") + 1);
}
}
std::ofstream outputFile("Resultsfor_" + std::to_string(data.threadID) + ".csv", std::ios_base::app);
if (!outputFile.is_open()) {
std::ofstream errorFile("Resultsfor_Error.csv", std::ios_base::app);
errorFile << "Error creating/opening output CSV file." << std::endl;
return;
}
try {
// Establish database connection and execute query
SQLHENV hEnv;
SQLHDBC hDbc;
SQLRETURN retcode;
// Allocate environment handle
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
throw std::runtime_error("Error allocating environment handle.");
}
// Set the ODBC version environment attribute
retcode = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
throw std::runtime_error("Error setting ODBC version attribute.");
}
// Allocate connection handle
retcode = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
throw std::runtime_error("Error allocating connection handle.");
}
// Connect to the database
retcode = SQLDriverConnectA(hDbc, NULL, (SQLCHAR*)data.connectionString.c_str(), SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
throw std::runtime_error("Error connecting to the database.");
}
// Execute the query
SQLHSTMT hStmt;
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
throw std::runtime_error("Error allocating statement handle.");
}
retcode = SQLExecDirectA(hStmt, (SQLCHAR*)data.query.c_str(), SQL_NTS);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
throw std::runtime_error("Error executing query.");
}
// Process the query results
SQLSMALLINT numCols;
retcode = SQLNumResultCols(hStmt, &numCols);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
throw std::runtime_error("Error retrieving the number of result columns.");
}
// Write header
for (SQLSMALLINT i = 1; i <= numCols; ++i) {
SQLCHAR colName[256];
SQLSMALLINT colNameLength;
retcode = SQLDescribeColA(hStmt, i, colName, sizeof(colName), &colNameLength, NULL, NULL, NULL, NULL);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
outputFile << "Error describing column: " << i << std::endl;
continue;
}
outputFile << colName;
if (i < numCols) {
outputFile << ",";
}
}
outputFile << std::endl;
我已经确认我所拥有的信用是正确的,因为我能够通过 Excel 连接并提取数据。我尝试过不同版本的连接字符串,包括 DBQ 和其他版本
所以我相信我弄清楚了问题,timberline Data ODBC 只是 x32 位,这个 c++ 代码是为 x64 编译的,据我所知,那些人永远无法交谈。