home > OLE オートメーション > ADO >

ForNext

Only Do What Only You Can Do

13. 選択クエリー (Recordset.Open)

VBScript

JScript

Perl

PHP

Python

Ruby

PowerShell

Scala

F#

C

C++

C++Builder

更新日 : 2005.12.13
void __fastcall TForm1::Button14Click(TObject *Sender)
{
    const int adOpenDynamic         =  2;
    const int adOpenForwardOnly     =  0;
    const int adOpenKeyset          =  1;
    const int adOpenStatic          =  3;
    const int adOpenUnspecified     = -1;

    const int adLockBatchOptimistic =  4;
    const int adLockOptimistic      =  3;
    const int adLockPessimistic     =  2;
    const int adLockReadOnly        =  1;
    const int adLockUnspecified     = -1;

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

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

    //Edit3 に mdbファイル名を入力
    cn.Exec(PropertySet("ConnectionString")<<"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Edit3->Text + ";");
    cn.Exec(Procedure("Open"));

    //Edit4 に テーブル名を入力
    //Edit5, Edit6 に フィールド名を入力
    //Edit1 に Key値を入力
    AnsiString sql = "SELECT * FROM " + Edit4->Text + " "
                   + "WHERE "  + Edit5->Text + " = " + Edit1->Text;

    Variant rs = Variant::CreateObject("ADODB.Recordset");
    rs.Exec(PropertySet("Source")<<sql);
    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"));
}

VC++

C#

Java

Objective-C

D

VB

VB.NET

Delphi

更新日 : 2005.12.13
procedure TForm1.Button14Click(Sender: TObject);
var
    cn :Variant;//Connection
    rs :Variant;//Recordset
    sql:string; //Recordset
const
    adOpenDynamic:         Integer =  2;
    adOpenForwardOnly:     Integer =  0;
    adOpenKeyset:          Integer =  1;
    adOpenStatic:          Integer =  3;
    adOpenUnspecified:     Integer = -1;

    adLockBatchOptimistic: Integer =  4;
    adLockOptimistic:      Integer =  3;
    adLockPessimistic:     Integer =  2;
    adLockReadOnly:        Integer =  1;
    adLockUnspecified:     Integer = -1;
begin
    //表示用コントロールをクリア
    ListBox1.Items.Clear;
    ListBox2.Items.Clear;

    cn := CreateOleObject('ADODB.Connection');

    //Edit3 に mdbファイル名を入力
    cn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + Edit3.Text + ';';
    cn.Open;

    //Edit4 に テーブル名を入力
    sql := 'SELECT * FROM ' + Edit4.Text;
    rs := CreateOleObject('ADODB.Recordset');
    rs.Source           := sql;
    rs.Activeconnection := cn;
    rs.CursorType       := adOpenForwardOnly;
    rs.LockType         := adLockReadOnly;
    rs.Open;

    while not rs.EOF do
    begin
        //表示用コントロールに項目内容をセット
        ListBox1.Items.Add(rs.Fields[0].Value);
        ListBox2.Items.Add(rs.Fields[1].Value);

        rs.MoveNext;
    end;

    rs.Close;
    cn.Close;
end;

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system