C++快速开发例子1--ADO之Briefcase

本文介绍了一个使用Delphi编写的表单应用,展示了如何通过ADO.NET连接数据库、读写文件并实现数据的加载、更新和保存。重点在于FormMain类中ckbxConnectionInd和按钮事件的处理,适合数据库操作初学者学习。

1. Form设计

2. 源码

#include <vcl.h>
#pragma hdrstop

#include "BriefCaseMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TfrmMain *frmMain;

//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
        BaseFileName = "EMPLOYEE.ADTG";
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::LoadData()
{
        DataFileName = ExtractFilePath(ParamStr(0))+ BaseFileName;
        // If a persisted datafile exists, assume we exited in a disconnected
        // (offline) state and load the data from the file.
        if (FileExists(DataFileName))
        {
                adodsEmployees->LoadFromFile(DataFileName);
        }
        else
        {
                // Otherwise establish the connection and get data from the database
                ckbxConnectionInd->Checked = true;
                adodsEmployees->Open();
        }
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::UpdateData()
{
        // Connect to the database and send the pending updates }
        ckbxConnectionInd->Checked = true;
        adodsEmployees->UpdateBatch(arAll);
        DeleteFile(DataFileName);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SaveData()
{
        adodsEmployees->SaveToFile(DataFileName, pfADTG);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ckbxConnectionIndClick(TObject *Sender)
{
        if(ckbxConnectionInd->Checked)
        {
                // Toggle the connections state
                adocnctnConnection->Open("", "");
                adodsEmployees->Connection = adocnctnConnection;
                adodsEmployees->Open();
        }
        else
        {
                // Note here you must clear the connection property of the
                // dataset before closing the connection.  Otherwise the dataset
                // will close with the connection.
                adodsEmployees->Connection = NULL;
                adocnctnConnection->Close();
        }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnUpdateButtonClick(TObject *Sender)
{
        UpdateData();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnRefreshButtonClick(TObject *Sender)
{
        // Close and reopen the dataset to refresh the data.  Note that in this demo
        // there is no checking for pending updates so they are lost if you click
        // the refresh data button before clicking the Update database button.
        ckbxConnectionInd->Checked = true;
        adodsEmployees->Close();
        adodsEmployees->CommandType = cmdTable;
        adodsEmployees->CommandText = "Employee";
        adodsEmployees->Open();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSaveButtonClick(TObject *Sender)
{
        SaveData();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCloseQuery(TObject *Sender, bool &CanClose)
{
        if(adodsEmployees->Active)
        {
                try
                {
                // When closing, update the database if connected or save it to disk if not
                        if (adocnctnConnection->Connected)
                                UpdateData();
                        else
                                SaveData();
                }
                catch(Exception &E)
                {
                        Application->HandleException(Sender);
                        CanClose = MessageDlg("Data not saved/updated, exit anyway?", mtConfirmation,
                                TMsgDlgButtons() <<mbYes <<mbNo <<mbCancel, 0)== mbYes;
                }
        }
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
        adocnctnConnection->ConnectionString = "FILE NAME=" + DataLinkDir() + "\\BCDEMOS.UDL";
        LoadData();
}
//---------------------------------------------------------------------------


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moneytree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值