这是连接数据库的代码
void CEditDlg::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// TODO: 在此添加额外的初始化代码
CRect rect;
// 获取编程语言列表视图控件的位置和大小
n_standard.GetClientRect(rect);
// 为列表视图控件添加全行选中和栅格风格
n_standard.SetExtendedStyle(n_standard.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
// 为列表视图控件添加两列
n_standard.InsertColumn(0, _T("post"), LVCFMT_CENTER, rect.Width() / 2, 0);
n_standard.InsertColumn(1, _T("wages"), LVCFMT_CENTER, rect.Width() / 2, 1);
/*AfxOleInit();//初始化相应的Com环境*/
AfxEnableControlContainer();
m_pConnection = NULL;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->ConnectionString = "uid=;pwd=;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=F:/business/message.mdb;";
m_pConnection->Open(L"", L"", L"", adCmdUnspecified); //打开本地Access库message.mdb
_bstr_t ha = "select * from stand"; //查询表里的数据
m_pRecordset = m_pConnection->Execute(ha, NULL, adCmdText); //获取记录集
int i = 0;
while (!m_pRecordset->adoEOF)
{
n_post = (char*)(_bstr_t)m_pRecordset->GetCollect("post");
n_wages = (char*)(_bstr_t)m_pRecordset->GetCollect("wages");
n_standard.InsertItem(i, _T(""));
n_standard.SetItemText(i, 0, n_post); //添加到list Control 中的第i行的第一列下
n_standard.SetItemText(i, 1, n_wages); //添加到list Control 中的第i行的第二列下
i += 1; //下次循环指向第2行
m_pRecordset->MoveNext(); //移动到下一个记录
}
这是插入的代码
UpdateData(TRUE);//TRUE:控件值->变量,FALSE:变量->控件
if(n_post == "" || n_wages == "")
{
AfxMessageBox(_T("职务和工资信息不能为空!"));
return;
}
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误。
try
{
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("post", _variant_t(n_post));
m_pRecordset->PutCollect("wages", _variant_t(n_wages));
m_pRecordset->Update();
AfxMessageBox(_T("插入成功!"));
}
catch(_com_error& e)
{
e.ErrorMessage();
AfxMessageBox(_T("插入失败!"));
}
注销try catch后在addnew处报错