本文介绍了如何在VC ++控制台程序中使用SQLAPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将c ++控制台程序与SQL数据库链接,并且我已经从sqlapi.com下载了SQL API,但是将.lib放入其他依赖项之后,系统32中的DLL和标头中的SQLAPI.h仍然无法正常工作当我尝试在程序中包括sqlheader时,它显示错误,并且还显示sqlapi.dll的另一个错误?谁能向我解释在visualC ++中使用SQL API的过程.
/thanks事先
I am trying to link my c++ console program with the SQL database and i have downloaded the SQL API from sqlapi.com but after putting .lib in additional dependencies,DLL in system 32 and SQLAPI.h in header it still dont work and when i try to include the sqlheader in my program it shows error and also it shows another error of sqlapi.dll? can anybody explain me the process to use SQL API in visualC++.
/thanks in advance
推荐答案
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
int main(void)
{
HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL; // Statement handle
UCHAR szDSN[SQL_MAX_DSN_LENGTH] ="db97"; // Data Source Name buffer
UCHAR* szUID = NULL; // User ID buffer
UCHAR* szPasswd = NULL;// Password buffer
UCHAR szModel[128]; // Model buffer
SDWORD cbModel; // Model buffer bytes recieved
UCHAR szSqlStr[] = "Select Model FromMakes Where Make=''Vauxhall''"; // SQL string
RETCODE retcode; // Return code
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "db97" using userid andpassword.
retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID,SQL_NTS, szPasswd, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode ==SQL_SUCCESS_WITH_INFO)
{
// Allocate memory for the statementhandle
retcode = SQLAllocStmt (hDBC, &hStmt);
// Prepare the SQL statement by assigningit to the statement handle
retcode = SQLPrepare (hStmt, szSqlStr,sizeof (szSqlStr));
// Execute the SQL statement handle
retcode = SQLExecute (hStmt);
// Project only column 1 which is themodels
SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel,sizeof(szModel), &cbModel);
// Get row of data from the result setdefined above in the statement
retcode = SQLFetch (hStmt);
while (retcode == SQL_SUCCESS || retcode== SQL_SUCCESS_WITH_INFO)
{
printf ("%s",szModel); // Print row(model)
retcode = SQLFetch(hStmt); // Fetch next row from result set
}
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
// Disconnect from datasource
SQLDisconnect (hDBC);
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);
return 0;
}
这篇关于如何在VC ++控制台程序中使用SQLAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!