`
kuwoleft
  • 浏览: 1074754 次
文章分类
社区版块
存档分类
最新评论

TestComplete对数据库的支持

 
阅读更多

TestComplete对数据库的支持

陈能技

2007-9-7

在自动化测试中,有时候需要对数据库进行操作,例如,从数据库读取测试数据、检查软件对数据库的更改操作是否正确等等。TestComplete提供好几种对数据库进行操作的方法。

TC的三类数据库操作方法

1、通常开发人员会使用特殊的控件或组件(例如Microsoft ADO Components)来与数据库打交道。如果这些组件是ActiveX控件,则你也可以在TC中使用它们。你可以在TC项目中添加ActiveX Object项目Item,并把数据组件加入。然后就可以在ActiveX Object editor中设置控件属性,定义事件处理,然后在脚本中像VC++VB中一样使用这些控件的方法和属性。

2、使用windowsADO数据库引擎来连接数据库。你可以把连接、数据集和命令作为COM对象在脚本中使用它的方法和属性。

3、使用TCADOBDE编程对象的属性和方法来操作数据库。

微软ADO DB对象的使用

下面脚本实例化一个ADO DB对象,然后遍历数据库的一个表:

procedure TestADO;
var
Conn, Rs, Fldr : OleVariant;
begin
Fldr := Log.CreateFolder('Authors table');
Log.PushLogFolder(Fldr);

//
创建并打开数据库连接
Conn := Sys.OleObject['ADODB.Connection'];
Conn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.3.51;' +
'Data Source=C:/Program Files/Microsoft Visual Studio/VB98/biblio.mdb';
Conn.Open();

//
创建并打开一个数据集
Rs := Sys.OleObject['ADODB.Recordset'];
Rs.Open('Authors', Conn, 3 {adOpenStatic},
1 {adLockReadOnly}, 2 {adCmdTable});
//
处理数据
Rs.MoveFirst();
while not Rs.EOF do
begin
Log.Message(Rs.Fields.Item('Author').Value);
Rs.MoveNext();
end;

//
关闭数据集和连接
Rs.Close();
Conn.Close();
end;

使用BDE操作数据库

BDEBorland Database Engine,要使用BDE,首先要创建一个DSN连接,在操作系统的控制面板的管理工具找到数据源(ODBC)并打开,然后创建一个BDE别名,选择一个数据库驱动。

下面脚本使用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 := '';
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等更新的数据库驱动的使用,通过TCOpen Application机制,我们可以直接调用.NET的库,使用.NETSystem.Data命名空间下的ADO.NET的所有类和方法。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics