ODBC

2018-03-19  本文已影响14人  downdemo

几个常用类

构建数据源

关联编辑框控件和数据

菜单项更新和删除

void CMy10View::OnDeleteRecord()
{
    // TODO: Add your command handler code here
    CRecordsetStatus m_cStatus;
    try{ m_pSet->Delete();  }
    catch (CDBException* m_pEx)
    {
        AfxMessageBox(m_pEx->m_strError);
        m_pEx->Delete();
        m_pSet->MoveFirst(); // 失败则将记录指针移到开头
        UpdateData(FALSE);
        return;
    }

    m_pSet->GetStatus(m_cStatus);
    if (m_cStatus.m_lCurrentRecord == 0)
        m_pSet->MoveFirst();
    else
        m_pSet->MoveNext();
    UpdateData(FALSE);
}
void CMy10View::OnUpdateDeleteRecord(CCmdUI *pCmdUI)
{
    // TODO: Add your command update UI handler code here
    pCmdUI->Enable(!m_pSet->IsEOF());
}
void CMy10View::OnUpdateRecord()
{
    // TODO: Add your command handler code here
    m_pSet->Edit();
    UpdateData(TRUE);
    if (m_pSet->CanUpdate()) // 记录可更新则返回非零值
        m_pSet->Update();
}
void CMy10View::OnUpdateUpdateRecord(CCmdUI *pCmdUI)
{
    // TODO: Add your command update UI handler code here
    pCmdUI->Enable(!m_pSet->IsEOF());
}
void CMy10View::OnClearDomain()
{
    // TODO: Add your command handler code here
    m_pSet->SetFieldNull(NULL);
    UpdateData(FALSE);
}

重载OnMove

BOOL CMy10View::OnMove(UINT nIDMoveCommand)
{
    // TODO: Add your specialized code here and/or call the base class
    switch (nIDMoveCommand)
    {
        case ID_RECORD_PREV:
            if (!m_pSet->IsBOF())
                m_pSet->MovePrev();
            break;
        case ID_RECORD_FIRST:
            m_pSet->MoveFirst();
            break;
        case ID_RECORD_NEXT:
            if (!m_pSet->IsEOF())
                m_pSet->MoveNext();
            break;
        case ID_RECORD_LAST:
            m_pSet->MoveLast();
            break;
        default:
            ASSERT(FALSE);
    }
    UpdateData(FALSE);
    return CRecordView::OnMove(nIDMoveCommand);
}

菜单项添加

int CMy10Set::GetMaxID()
{
    MoveLast(); // 移到最后一条记录
    return m_ID;
}
void CMy10View::OnAddRecord()
{
    // TODO: Add your command handler code here
    CRecordset* pSet = OnGetRecordset(); // 获取指向数据库的指针
    if (pSet->CanUpdate() && !pSet->IsDeleted())
    {
        pSet->Edit();
        if (!UpdateData()) return;
        pSet->Update();
    }
    long m_INewID = m_pSet->GetMaxID() + 1; // 获取新ID值
    m_pSet->AddNew(); // 添加一个新记录
    m_pSet->m_ID = m_INewID;
    m_pSet->Update(); // 保存新记录
    m_pSet->Requery(); // 刷新数据库
    m_pSet->MoveLast();
    UpdateData(FALSE); // 更新表单
}

菜单项浏览和排序

void CMy10View::OnMovetorecord()
{
    // TODO: Add your command handler code here
    CMoveToRecord dlgMoveTo;
    if (dlgMoveTo.DoModal() == IDOK)
    {
        CRecordset *pSet = OnGetRecordset();
        pSet->SetAbsolutePosition(dlgMoveTo.m_RecordID);
        UpdateData(FALSE);
    }
}
void CMy10View::OnSortPrice()
{
    // TODO: Add your command handler code here
    // TODO: Add your command handler code here
    m_pSet->Close(); // 关闭数据库
    m_pSet->m_strSort = "价格";
    m_pSet->Open();
    UpdateData(FALSE); // 更新已排序过的记录
}
上一篇下一篇

猜你喜欢

热点阅读