下面腳本使用BDE對象連接到一個別名為MYSQL的數據庫的Products表,打開這個表取出字段名,然后遍歷整個表取出所有記錄。
procedure TestSQL_BDE;
var
aTable, S, i : OleVariant;
begin
// Create a table
aTable := BDE.CreateTable;
// Specify the database name
aTable.DatabaseName := 'MYSQL'; // <-- BDE alias
// Specify the table name
aTable.TableName := 'Products';
// Open the table
aTable.Open;
aTable.First;
// Retrieve field names
S := '';
for i := 0 to aTable.FieldCount - 1 do
S := S + aTable.Field(i).FieldName + Chr(9);
S := S + Chr(13) + Chr(10);
// Scan through dataset records
while not VarToBool(aTable.EOF) do
begin
for i := 0 to aTable.FieldCount - 1 do
S := S + aTable.Field(i).AsString + Chr(9);
S := S + Chr(13) + Chr(10);
aTable.Next;
end;
// Output results
Log.Message('Products', S);
// Close the table
aTable.Close;
end;
使用ADO對象操作數據庫
ADO,即ActiveX Data Object,使用ADO同樣需要先創建ODBC連接。
TC支持兩種使用ADO的方法,一種是跟Delphi ADO對象一致的方法,一種是按照ADO本身的使用方法。
下面把兩種方法都列出。
//類似Delphi ADO的使用方法
procedure TestSQL_ADO;
var
aTable, S, i : OleVariant;
begin
// Creates a table
aTable := ADO.CreateADOTable();
// Specifies the database name
aTable.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;Data Source=NameOfMyDSN';
// Specifies the table name
aTable.TableName := 'Products';
// Opens the table
aTable.Open;
aTable.First;
// Retrieves field names
S := '';
文章來源于領測軟件測試網 http://www.kjueaiud.com/