Win32 Sdk 连接Access数据库
作者:互联网
/************************************************************* *** MyWinClass.cpp 创建窗口模板 *** **************************************************************/ #import "c://program files//common files//system//ado//msado15.dll" no_namespace rename("EOF","adoEOF") //导入ADO库 #include <Windows.h> #include <tchar.h> #include <comutil.h> #include <commctrl.h> #include "resource.h" // 必须要进行前导声明 INT_PTR CALLBACK WindowProc(_In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam); void LoadDataToListCtrl(HWND hDlg); // 程序入口点 int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow ) { // 创建窗口 HWND hwnd = CreateDialog(hInstance, (LPCTSTR)IDD_DIALOG1, NULL, WindowProc); if (hwnd == NULL) //检查窗口是否创建成功,回调函数要返回个0,不然创建不会成功 return 0; // 显示窗口 ShowWindow(hwnd, SW_SHOW); // 更新窗口 UpdateWindow(hwnd); // 消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }
// 在WinMain后实现 INT_PTR CALLBACK WindowProc( _In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam) { HDC hdc; RECT rect; PAINTSTRUCT ps; //int k; switch (uMsg) { /*case WM_SYSCOMMAND: break;*/ case WM_INITDIALOG: { LoadDataToListCtrl(hwnd); return 0; } case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CLOSE: if (MessageBox(hwnd, L"请问是否真的要关闭窗口?", L"窗口关闭询问", MB_YESNO) == IDYES) { DestroyWindow(hwnd); return 0; } else return 0; case WM_LBUTTONDOWN: { //MessageBox(hwnd, L"哎呀,我丫的被按了一下", TEXT("好舒服~"), MB_OK); return 0; } case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, L"Hello world!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hwnd, &ps); return 0; } return false; } //加载数据库数据到ListCtrl控件中 void LoadDataToListCtrl(HWND hDlg) { //查询数据库文件是否存在 LPCTSTR DatabaseFile = L"D:\\MyAccessFile\\myTonxilu.accdb"; WIN32_FIND_DATA wfd; HANDLE hFile = FindFirstFile(DatabaseFile, &wfd); if (hFile == INVALID_HANDLE_VALUE) { wprintf_s(L"Cannot Find the database file/n");//这名是啥意思,我也不知道,俺是抄的(: //system("pause"); return; }
//初始化ADO环境 CoInitialize(NULL);
//创建数据库连接对像,并打开 _ConnectionPtr m_Connection; m_Connection.CreateInstance(__uuidof(Connection)); HRESULT hr = m_Connection->Open(_bstr_t("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\MyAccessFile\\myTonxilu.accdb"), _bstr_t(""), _bstr_t(""), adModeUnknown); if (hr != S_OK) return; //连接记录集 _RecordsetPtr m_Recordset; m_Recordset.CreateInstance(__uuidof(Recordset)); HRESULT hRs = m_Recordset->Open(_variant_t(L"select * from mTable"), m_Connection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); if (hRs != S_OK) return; //获得ListCtrl控件的句柄 HWND hList = GetDlgItem(hDlg, IDC_LIST1);
//设置风格样式 SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); RECT rt; ::GetClientRect(hList, &rt); int nListWith = rt.right - 20; //获得控件的宽度
//插入列标题 LVCOLUMN col = { 0 }; col.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH; col.pszText = (LPTSTR)L"编号"; col.fmt = LVCFMT_LEFT; // 列文字排列样式,居左,中,右 col.cx = (int)(0.2*nListWith);// 列的宽度 col.iSubItem = 0; SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col); col.iSubItem = 1; col.pszText = (LPTSTR)L"姓名"; col.cx = (int)(0.2*nListWith); SendMessage(hList, LVM_INSERTCOLUMN, 1, (LPARAM)&col); col.iSubItem = 2; col.pszText = (LPTSTR)L"性别"; col.cx = (int)(0.15*nListWith); SendMessage(hList, LVM_INSERTCOLUMN, 2, (LPARAM)&col); //循环加入行数据
int i = 0;
while (!m_Recordset->adoEOF)
{
LVITEM lv = { 0 };
lv.mask = LVCF_TEXT | LVCF_FM ;
lv.iItem = i;
lv.iSubItem = 0; TCHAR str[256]; _stprintf_s(str, L"%s", (LPCTSTR)_bstr_t(m_Recordset->GetCollect(_T("ID")))); lv.pszText = str; SendMessage(hList, LVM_INSERTITEM, i, (LPARAM)&lv); lv.iSubItem = 1; _stprintf_s(str, L"%s", (LPCTSTR)_bstr_t(m_Recordset->GetCollect(_T("mName")))); lv.pszText = str; SendMessage(hList, LVM_SETITEMTEXT, i, (LPARAM)&lv); lv.iSubItem = 2; _stprintf_s(str, L"%s", (LPCTSTR)_bstr_t(m_Recordset->GetCollect(_T("mSex")))); lv.pszText = str; SendMessage(hList, LVM_SETITEMTEXT, i, (LPARAM)&lv); m_Recordset->MoveNext(); i++; }
//关闭记录集并释放资源 m_Recordset->Close(); m_Recordset.Release(); m_Recordset = NULL;
//关闭数据库连接并释放资源 m_Connection->Close(); m_Connection.Release(); m_Connection = NULL;
//释放环境 CoUninitialize(); }
标签:Access,return,Recordset,hwnd,Win32,hList,lv,col,Sdk 来源: https://www.cnblogs.com/greenleaf1976/p/12913603.html