MFC连接Sqlserver - osbreak
程序员文章站
2022-03-16 17:42:05
...
下载 ado2.h和ado2.cpp文件
在VC++ 目录-->包含目录 -->添加 msado15.dll, msjro.dll 目录。
// TODO: 连接sqlserver, 在stdafx.h 中定义这些,#define _BS_DLL_EXPORT_# ifdef _BS_DLL_EXPORT_ # define BS_DLL_EXPORT __declspec(dllexport) # else# define BS_DLL_EXPORT __declspec(dllimport) # endif
连接sqlserver数据库bool connectToSqlServer() { CADODatabase *g_pAdoDatabase; try{ if (g_pAdoDatabase == NULL) g_pAdoDatabase = new CADODatabase(); CString strConnString = "Provider=SQLOLEDB;Persist Security Info=False;Data Source=" + \ strServer + ";Initial Catalog=" + strDatabase + ";User Id=" + strUser + ";Password=" + strPwd; g_pAdoDatabase->SetConnectionString((LPCTSTR)strConnString); } catch (...) { return false; } return true; }
if(g_pAdoDatabase->Open()) { // 查询 CString sqlText = "select ..."; CADORecordset* pRs = new CADORecordset(g_pAdoDatabase); if(pRs->Open((LPCTSTR)sqlText)) { while (!pRs->IsEof()) { pRs->GetFieldValue("id", ID); pRs->MoveNext(); } } pRs->Close(); delete pRs; }catch (...) { return false; }if(g_pAdoDatabase->IsOpen()) { g_pAdoDatabase->Close(); }
下一篇: PHP自动生成表单的方法