for i := 0 to aTable.FieldCount - 1 do
S := S + aTable.Field(i).FieldName + Chr(9);
S := S + Chr(13) + Chr(10);
// Scans 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;
// Outputs results
Log.Message('Products', S);
// Closes the table
aTable.Close;
end;
// “原生”ADO的使用方法
procedure TestSQL_ADO2;
var
aCon, aCmd, aRecSet, S, i : OleVariant;
begin
// Creates ADO connection
aCon := ADO.CreateConnection;
// Sets up the connection parameters
aCon.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;Data Source=NameOfMyDSN';
// Opens the connection
aCon.Open;
// Creates a command and specifies its parameters
aCmd := ADO.CreateCommand;
aCmd.ActiveConnection := aCon; // Connection
aCmd.CommandType := adCmdTable; // Command type
aCmd.CommandText := 'Products'; // Table name
// Opens a recordset
aRecSet := aCmd.Execute;
aRecSet.MoveFirst;
// Obtains field names
s := '';
for i := 0 to aRecSet.Fields.Count - 1 do
s := s + aRecSet.Fields.Item(i).Name + Chr(9);
s := s + Chr(13) + Chr(10);
// Scans recordset
while not aRecSet.EOF do
begin
for i := 0 to aRecSet.Fields.Count - 1 do
s := s + VarToString(aRecSet.Fields.Item(i).Value) + Chr(9);
s := s + Chr(13) + Chr(10);
aRecSet.MoveNext;
end;
// Outputs results
Log.Message('Products', s);
// Closes the recordset and connection
aRecSet.Close;
aCon.Close;
end;
扩展
实际上,除了上面说的三类使用方法外,TC还支持ADO.NET等更新的数据库驱动的使用,通过TC的Open Application机制,我们可以直接调用.NET的库,使用.NET的System.Data命名空间下的ADO.NET的所有类和方法。
文章来源于领测软件测试网 https://www.ltesting.net/