テーブルタイプのレコードセットを使ってJetデータベースのテーブルにアクセスします。テーブルタイプはキーを指定してアクセスするので動作が高速です。
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <dbdaoint.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i;
int j;
Variant vDAO;
Variant vDB;
Variant vRS;
AnsiString str = ExtractFilePath(Application->ExeName) + "TEST.mdb";
vDAO = Variant::CreateObject("DAO.DBEngine.35");
vDB = vDAO.Exec(Function("OpenDatabase") << str);
vRS = vDB.Exec(Function("Openrecordset") << "新規テーブル" << dbOpenTable);
vDAO.Exec(Function("BeginTrans"));
for (i=1;i<11;i++){
vRS.Exec(PropertySet("Index") << "Primarykey");
vRS.Exec(Procedure("Seek") << "=" << "Key" + IntToStr(i) << i);
if(vRS.Exec(PropertyGet("NoMatch"))){
vRS.Exec(Function("AddNew"));
vRS.Exec(PropertySet("FieldText") << "Key" + IntToStr(i));
vRS.Exec(PropertySet("FieldInteger") << i);
vRS.Exec(PropertySet("FieldMemo") << "AddNew");
vRS.Exec(Function("Update"));
}else{
vRS.Exec(Function("Edit"));
vRS.Exec(PropertySet("FieldText") << "Key" + IntToStr(i));
vRS.Exec(PropertySet("FieldInteger") << i);
vRS.Exec(PropertySet("FieldMemo") << "Edit");
vRS.Exec(Function("Update"));
}
}
vDAO.Exec(Function("CommitTrans"));
//指定したキー以降のレコードを取得し、結果をリストボックスに出力します。
ListBox1->Items->Clear();
vRS.Exec(PropertySet("Index") << "Primarykey");
vRS.Exec(Procedure("Seek") << ">=" << "Key5" << 5);
while (!vRS.Exec(PropertyGet("EOF"))){
ListBox1->Items->Add(vRS.Exec(PropertyGet("FieldText")));
vRS.Exec(Function("MoveNext"));
}
vRS.Exec(Procedure("Close"));
vDB.Exec(Procedure("Close"));
}
//---------------------------------------------------------------------------
DownLoad bcbtips054.lzh 8KB(BCB5)