SQL Server C/C++ Connect SQLSERVER

{

 

#include <icrsint.h>
#include <windows.h>  
#include <sqltypes.h>  
#include <sql.h>  
#include <sqlext.h>
//方式一
#pragma comment(lib,"odbc32.lib")
//方式二
//#import "c:\program files\common files\system\ado\msado15.dll"  no_namespace rename("EOF", "adoEOF")

//方式一:ODBC////////////////////华丽的分割线/////////////////////////////////////
SQLHENV env;        //environment handle  
SQLHDBC dbc;        //connection handle  
SQLHSTMT stmt;      //state handle  
SQLRETURN ret;      //result return  
 
//查询的结果返回到这些变量里  
SQLCHAR sno[64];  
SQLINTEGER grade;  
SQLINTEGER cbSno , cbGrade ;

//环境句柄
ret = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &env);     
ret = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
ret = SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
char c[512];
sprintf_s(c,"DRIVER={SQL Server};SERVER=192.168.1.29;DATABASE=baoshang;UID=sa;PWD=ebank@123;");

SQLCHAR outstr[1024];
SQLSMALLINT outstrlen;
ret = SQLDriverConnect(dbc, NULL, (SQLCHAR *)c, SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);

int intTemp = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);  
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt))  
{  
AfxMessageBox("Failed to connect");
}  
else  
{  
// 基本上每个SQLExecDirect()都要初始化句柄 
ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);  
ret = SQLSetStmtAttr(stmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER)SQL_BIND_BY_COLUMN, SQL_IS_INTEGER);  

//查询  
ret = SQLExecDirect(stmt, (SQLCHAR *)("SELECT * FROM dbo.TB_DICT_CONTTYPE"), SQL_NTS);  
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)  
{  
/*  句柄、列、变量类型、接收缓冲、缓冲长度、返回的长度   */  
ret = SQLBindCol(stmt, 1, SQL_C_LONG, &grade, 0, &cbGrade);  
//ret = SQLBindCol(stmt, 2, SQL_C_CHAR, cno, CNO_LEN, &cbCno);  
ret = SQLBindCol(stmt, 3, SQL_C_CHAR, sno, 64, &cbSno);  
}  
//遍历数据  
while ((ret = SQLFetch(stmt)) != SQL_NO_DATA_FOUND)  
{  
if (ret == SQL_ERROR)   printf("fetch error\n");  
else  
{  
char szS[128];
sprintf_s(szS,"%d:%s",grade,sno);
AfxMessageBox(szS);
}  
}   
SQLFreeHandle(SQL_HANDLE_STMT, stmt);  
SQLDisconnect(dbc);  
SQLFreeHandle(SQL_HANDLE_DBC, dbc);  
SQLFreeHandle(SQL_HANDLE_ENV, env);  
}


//方式二:ADO/////////////////////////华丽的分割线///////////////////////////////////////////////////////////////////
_ConnectionPtr  sqlSp;
HRESULT hr=sqlSp.CreateInstance(_uuidof(Connection));

if(FAILED(hr))
{
AfxMessageBox("_ConnectionPtr对象指针实例化失败!!!");
return ;
}
else {
try {
_bstr_t strConnect="Driver={sql server};server=192.168.1.29;uid=sa;pwd=ebank@123;database=baoshang;";
sqlSp->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error &e) 
{
AfxMessageBox(e.Description());
return;
}
_RecordsetPtr m_pRecordset;
if(FAILED(m_pRecordset.CreateInstance( _uuidof( Recordset ))))
{
AfxMessageBox("记录集对象指针实例化失败!");
return;
}
try {
m_pRecordset->Open("select * from  dbo.TB_DICT_CONTTYPE",(IDispatch*)sqlSp,adOpenDynamic,adLockOptimistic, adCmdText);
}
catch (_com_error &e)
{
AfxMessageBox(e.Description());
return;
}
try {
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF) 
{
CString insertTime=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("F_TYPE_ID"))->Value);
AfxMessageBox(insertTime);
m_pRecordset->MoveNext();
}
}
catch (_com_error &e)
{
AfxMessageBox(e.Description());
return;
}
}

 

}

posted @ 2020-04-08 22:12  YZFHKMS-X  阅读(431)  评论(0编辑  收藏  举报