ODBC 小例

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <locale>
#include <string.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>

using namespace std;
SQLHENV henv = SQL_NULL_HENV; //定义环境句柄
SQLHDBC hdbc1 = SQL_NULL_HDBC; //定义数据库连接句柄
SQLHSTMT hstmt1 = SQL_NULL_HSTMT; //定义语句句柄
int main()
{
RETCODE retcode;
retcode = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);
if (retcode < 0)
{
cout << "allocate ODBC Environment handle errors." << endl;
return -1;
}
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
if (retcode < 0)
{
cout << "the ODBC is not version3.0 " << endl;
return -1;
}
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
if (retcode < 0)
{
cout << "allocate ODBC connection handle errors." << endl;
return -1;
}

char* szDSN = "SQLSERVER_ODBC_USER";
char* szUID = "alision";
char* szAuthStr = "880206";
retcode = SQLConnect(hdbc1, (SQLCHAR*)szDSN, (SWORD)strlen(szDSN), (SQLCHAR*)szUID, (SWORD)strlen(szUID), (SQLCHAR*)szAuthStr, (SWORD)strlen(szAuthStr));
if (retcode < 0)
{
cout << "connect to ODBC datasource errors." << endl;
return -1;
}
cout << "connect success!" << endl;
system("pause");
}

上一篇:Oracle 11g+oracle客户端(32位)+PL/SQL develepment的安装配置


下一篇:WPF编游戏系列 之四 用户控件