C++Builder Personal で ADO

テキストファイルを読む


void __fastcall TForm1::Button21Click(TObject *Sender)
{
const int adOpenForwardOnly = 0;
const int adLockReadOnly = 1;

//表示用コントロールをクリア
ListBox1->Items->Clear();
ListBox2->Items->Clear();

Variant cn = Variant::CreateObject("ADODB.Connection");

//Edit3 に テキストファイルのパスを入力
cn.Exec(PropertySet("ConnectionString")<<"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" + Edit3->Text + ";");
cn.Exec(Procedure("Open"));

//Edit4 に テキストファイル名を入力
Variant rs = Variant::CreateObject("ADODB.Recordset");
rs.Exec(PropertySet("Source")<<Edit4->Text);
rs.Exec(PropertySet("Activeconnection")<<cn);
rs.Exec(PropertySet("CursorType")<<adOpenForwardOnly);
rs.Exec(PropertySet("LockType")<<adLockReadOnly);
rs.Exec(Procedure("Open"));

while (!rs.Exec(PropertyGet("EOF")))
{
//表示用コントロールに項目内容をセット
Variant fd = rs.Exec(PropertyGet("Fields")<<0);
ListBox1->Items->Add(fd.Exec(PropertyGet("Value")));

fd = rs.Exec(PropertyGet("Fields")<<1);
ListBox2->Items->Add(fd.Exec(PropertyGet("Value")));

rs.Exec(Function("MoveNext"));
}

rs.Exec(Procedure("Close"));
cn.Exec(Procedure("Close"));
}