工控编程吧
标题:
上位机MFC如何使用ADO执行SQL命令
[打印本页]
作者:
qq263946146
时间:
2019-8-9 11:20
标题:
上位机MFC如何使用ADO执行SQL命令
(, 下载次数: 1)
上传
点击文件名下载附件
当前这里准备一个例程。
首先是自定义 一些变量与函数,再如界面准备一些控件。
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
BOOL OpenDatabase(LPCTSTR lpszConnect, long nOptions=adConnectUnspecified);
BOOL CloseDatabase();
BOOL OpenRecordset(LPCTSTR lpszSource, long nCursorType=adOpenDynamic,
long nLockType=adLockOptimistic, long nOptions=adCmdText);
BOOL CloseRecordset();
复制代码
然后初始化这些变量与控件,与实现函数体。
if (!SUCCEEDED(m_pConnection.CreateInstance(__uuidof(Connection))))
{
m_pConnection = NULL;
TRACE(_T("Database CreateInstance failed"));
}
if (!SUCCEEDED(m_pRecordset.CreateInstance(__uuidof(Recordset))))
{
m_pRecordset = NULL;
TRACE(_T("Recordset CreateInstance Failed!"));
}
if (!SUCCEEDED(m_pCommand.CreateInstance(__uuidof(Command))))
{
m_pCommand = NULL;
TRACE(_T("Command CreateInstance Failed!"));
}
//打开数据库
CString strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= Demo.mdb");
if (!OpenDatabase(strConnect))
{
AfxMessageBox(_T("数据库打开失败。"));
}
m_pCommand->ActiveConnection = m_pConnection;
//初始化ListCtrl
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);
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);
SetDlgItemText(IDC_COMMAND, _T("SELECT * FROM DemoTable"));
复制代码
BOOL CGkbc8Dlg::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 CGkbc8Dlg::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 CGkbc8Dlg::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 CGkbc8Dlg::CloseRecordset()
{
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;
}
}
复制代码
记得列表控件设置属性为报表
(, 下载次数: 1)
上传
点击文件名下载附件
最后就是调用按钮执行SQL命令。
void CGkbc8Dlg::OnButton3()
{
CString strCommand = _T("");
GetDlgItemText(IDC_COMMAND, strCommand);
//执行SQL命令
m_pCommand->CommandText = _bstr_t(strCommand);
_variant_t vNULL;
vNULL.vt = VT_ERROR;
vNULL.scode = DISP_E_PARAMNOTFOUND;
try
{
m_pRecordset = m_pCommand->Execute(&vNULL, &vNULL, adCmdText);
}
catch (_com_error& e)
{
TRACE(_T("%s\n"), e.ErrorMessage());
return;
}
//读取记录集
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
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);
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