工控编程吧
标题:
上位机MFC如何使用ADO在数据库中修改记录
[打印本页]
作者:
qq263946146
时间:
2019-8-9 12:10
标题:
上位机MFC如何使用ADO在数据库中修改记录
(, 下载次数: 2)
上传
点击文件名下载附件
上面是例程截图。
初始化COM库 if (!SUCCEEDED(CoInitialize(NULL))) {
::AfxMessageBox(_T("Failed to initialize COM!"));
return FALSE;
}
加载ODA库
#import "c:\program files\common files\system\ado\msado15.dll"\
no_namespace\
rename("EOF", "adoEOF")
自定义数据库操作变量与函数
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
BOOL OpenDatabase(LPCTSTR lpszConnect, long nOptions=adConnectUnspecified);
BOOL CloseDatabase();
BOOL OpenRecordset(LPCTSTR lpszSource, long nCursorType=adOpenDynamic,
long nLockType=adLockOptimistic, long nOptions=adCmdText);
BOOL CloseRecordset();
BOOL CDemoDlg::OpenDatabase(LPCTSTR lpszConnect, long nOptions)
{
ASSERT(m_pConnection != NULL);
ASSERT(lpszConnect != NULL);
ASSERT(AfxIsValidString(lpszConnect));
//打开数据库连接
try
{
return SUCCEEDED(m_pConnection->Open(_bstr_t(lpszConnect),
_T(""), _T(""), nOptions));
}
catch (_com_error& e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
BOOL CDemoDlg::CloseDatabase()
{
ASSERT(m_pConnection != NULL);
//关闭数据库连接
try
{
if (m_pConnection->State & adStateOpen)
{
return SUCCEEDED(m_pConnection->Close());
}
else
{
return TRUE;
}
}
catch (_com_error& e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
BOOL CDemoDlg::OpenRecordset(LPCTSTR lpszSource, long nCursorType, long nLockType, long nOptions)
{
ASSERT(m_pConnection != NULL);
ASSERT(m_pRecordset != NULL);
ASSERT(lpszSource != NULL);
ASSERT(AfxIsValidString(lpszSource));
//打开记录集
try
{
return (SUCCEEDED(m_pRecordset->Open(_variant_t(lpszSource),
m_pConnection.GetInterfacePtr(),
(CursorTypeEnum)nCursorType,
(LockTypeEnum)nLockType,
nOptions)));
}
catch(_com_error e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
BOOL CDemoDlg::CloseRecorset()
{
ASSERT(m_pRecordset != NULL);
//关闭记录集
try
{
if (m_pRecordset->State & adStateOpen)
{
return SUCCEEDED(m_pRecordset->Close());
}
else
{
return TRUE;
}
}
catch (_com_error e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return FALSE;
}
}
复制代码
添加按钮与列表框等控件,并初始化。
//初始化ListCtrl
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
pListCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
pListCtrl->InsertColumn(1, _T("序号"), LVCFMT_CENTER, 80);
pListCtrl->InsertColumn(2, _T("姓名"), LVCFMT_CENTER, 100);
pListCtrl->InsertColumn(3, _T("年龄"), LVCFMT_CENTER, 100);
//读取记录集
if (m_pRecordset->BOF && m_pRecordset->adoEOF)
{
return FALSE;
}
m_pRecordset->MoveFirst();
int n = 0;
while (!m_pRecordset->adoEOF)
{
_variant_t varValue;
CString strName = _T("");
int nAge = 0;
varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME")));
if (varValue.vt != VT_NULL)
{
strName = varValue.bstrVal;
}
else
{
strName = _T("");
}
varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE")));
if (varValue.vt != VT_NULL)
{
nAge = varValue.intVal;
}
else
{
nAge = 0;
}
//刷新ListCtrl
CString strText = _T("");
strText.Format(_T("%d"), n + 1);
pListCtrl->InsertItem(n, strText);
strText.Format(_T("%s"), strName);
pListCtrl->SetItemText(n, 1, strText);
strText.Format(_T("%d"), nAge);
pListCtrl->SetItemText(n, 2, strText);
n++;
m_pRecordset->MoveNext();
}
复制代码
两个按钮的函数
void CGkbc8Dlg::OnButton1()
{
if (!(m_pRecordset->State & adStateOpen))
{
AfxMessageBox((_T("记录集未打开。")));
return;
}
//获得记录集当前位置
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
if (pos == NULL)
{
return;
}
int nItem = pListCtrl->GetNextSelectedItem(pos);
CString strName = "工控编程吧";
long nAge = 5;
//修改记录集
if (m_pRecordset->BOF && m_pRecordset->adoEOF)
{
return;
}
m_pRecordset->MoveFirst();
m_pRecordset->Move((long)nItem);
m_pRecordset->PutCollect(_variant_t(_T("NAME")),
_variant_t(strName));
m_pRecordset->PutCollect(_variant_t(_T("AGE")),
_variant_t(nAge));
m_pRecordset->Update();
//读取记录集
pListCtrl->DeleteAllItems();
if (m_pRecordset->BOF && m_pRecordset->adoEOF)
{
return;
}
m_pRecordset->MoveFirst();
int n = 0;
while (!m_pRecordset->adoEOF)
{
_variant_t varValue;
CString strName = _T("");
int nAge = 0;
varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME")));
if (varValue.vt != VT_NULL)
{
strName = varValue.bstrVal;
}
else
{
strName = _T("");
}
varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE")));
if (varValue.vt != VT_NULL)
{
nAge = varValue.intVal;
}
else
{
nAge = 0;
}
//刷新ListCtrl
CString strText = _T("");
strText.Format(_T("%d"), n + 1);
pListCtrl->InsertItem(n, strText);
strText.Format(_T("%s"), strName);
pListCtrl->SetItemText(n, 1, strText);
strText.Format(_T("%d"), nAge);
pListCtrl->SetItemText(n, 2, strText);
n++;
m_pRecordset->MoveNext();
}
}
复制代码
void CGkbc8Dlg::OnRead()
{ if (!(m_pRecordset->State & adStateOpen))
{
AfxMessageBox((_T("记录集未打开。")));
return;
}
//读取记录集
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
pListCtrl->DeleteAllItems();
if (m_pRecordset->BOF && m_pRecordset->adoEOF)
{
return;
}
m_pRecordset->MoveFirst();
int n = 0;
while (!m_pRecordset->adoEOF)
{
_variant_t varValue;
CString strName = _T("");
int nAge = 0;
varValue = m_pRecordset->GetCollect(_variant_t(_T("NAME")));
if (varValue.vt != VT_NULL)
{
strName = varValue.bstrVal;
}
else
{
strName = _T("");
}
varValue = m_pRecordset->GetCollect(_variant_t(_T("AGE")));
if (varValue.vt != VT_NULL)
{
nAge = varValue.intVal;
}
else
{
nAge = 0;
}
//刷新ListCtrl
CString strText = _T("");
strText.Format(_T("%d"), n + 1);
pListCtrl->InsertItem(n, strText);
strText.Format(_T("%s"), strName);
pListCtrl->SetItemText(n, 1, strText);
strText.Format(_T("%d"), nAge);
pListCtrl->SetItemText(n, 2, strText);
n++;
m_pRecordset->MoveNext();
}
}
复制代码
(, 下载次数: 0)
上传
点击文件名下载附件
[MFC408]1[/MFC408]
[weixinlianxi]1[/weixinlianxi]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4