51Testing软件测试论坛

标题: TestComplete,ADO.NET使用例子 [打印本页]

作者: ZERONG.HE    时间: 2008-5-18 23:47
标题: TestComplete,ADO.NET使用例子
TestComplete中,连接数据库,做各种的查询操作,可使用ADOBDE(例子见TestComplete中创建ADOCommand),也可通过大家比较熟悉的ADO.NET进行,先对其的使用做了两个简单例子,把使用比较频繁的查询操作封装在一个函数里,很方便在需要使用的地方调用,增强代码的可读性,减少代码冗余及提高工作效率:
//  日期:2008-5-18

//  描述:指定表名及字段,查找预期值在表中的记录条数

//  参数:TableName,比较记录所在的表名;FieldName,比较的字段;FindValue,查找的值;sqlconStr,数据库连接串

//  返回值:符合的记录条数

function ISInDB_Count(TableName:string, FieldName:string, FindValue:string, sqlconStr:string):Integer;

var sqlcon, sqlcmd, sqlDataAdapter, ds, SqlString, count;

begin

  //创建数据库连接对象

  sqlcon := dotNET.system_Data_SqlClient.SqlConnection.zctor(sqlconStr);

  sqlcon.open();

  //构造SQL查询语句

  SqlString := 'Select * from ' + TableName + ' where ' + FieldName + ' = ''' + FindValue + '''';

  sqlcmd := dotNET.system_Data_SqlClient.SqlCommand.zctor_3(SqlString, sqlcon);

  sqlDataAdapter := dotNET.system_Data_SqlClient.SqlDataAdapter.zctor_2(sqlcmd);

  ds := dotNET.system_Data.DataSet.zctor;

  sqlDataAdapter.Fill(ds);

  //返回符合条件的记录

  count := ds.Tables.Item[0].Count;

  reuslt := count;

end;


//  日期:2008-5-18

//  描述:根据ID查找表中的记录,比较某字段的值是否等于预期值

//  参数:TableName,比较记录所在的表名;FieldName,比较的字段;CompareValue,比较的值;ID,比较值对应的IDsqlconStr,数据库连接串

//  返回值:0,相等;1,不相等;2,没有符合的记录

function DBCompareValue_ByID(TableName:string, FieldName:string, CompareValue:string, ID:string, sqlconStr:string):Integer;

var sqlcon, sqlcmd, sqlDataAdapter, ds, SqlString, count, i, temp;

begin

  //创建数据库连接对象

  sqlcon := dotNET.system_Data_SqlClient.SqlConnection.zctor(sqlconStr);

  sqlcon.open();

  //构造SQL查询语句,其中m_ID为表中ID的列

  SqlString := 'Select * from ' + TableName + ' where m_ID =''' + ID + '''';

  sqlcmd := dotNET.system_Data_SqlClient.SqlCommand.zctor_3(SqlString, sqlcon);

  sqlDataAdapter := dotNET.system_Data_SqlClient.SqlDataAdapter.zctor_2(sqlcmd);

  ds := dotNET.system_Data.DataSet.zctor;

  sqlDataAdapter.Fill(ds);

  //返回符合SQL语句条件的记录

  count := ds.Tables.Item[0].Count;

  //表中没有符合的记录,返回2

  if count = 0 then

  begin

    result := 2;

    exit;

  end

  else

  begin

    for i:=0 to count - 1 do

    begin

      //取出记录的值于CompareValue比较是否相等

      temp := ds.Tables.Item[0].Columns.Item_2(FieldName).Item.OleValue;

      if CompareStr(temp, CompareValue) = 0 then

      begin

        result := 0;

        exit;

      end;

    end;

  end;

  Reuslt := 1;

end;

注:必要时参照以上例子,可以封装很多类似的数据库操作函数,在自动化过程中调用。 更多信息




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2