Connecting to a Microsoft SQL Server Database Using BDE

Applies to TestComplete 15.70, last modified on December 17, 2024
Accessing databases by using the Borland Database Engine (BDE) is deprecated. Do not use it to create new tests. It will be removed from the product in one of the future releases. As an alternative, you can access databases by using the Microsoft Active Data Object (ADO). See Using ADO Components.

The script sample below shows how to use the Borland Database Engine (BDE) to connect to a database managed by Microsoft SQL Server. The sample creates a BDE table that stores data from the Products table of the Northwind sample database that is shipped with the Microsoft SQL Server.

Requirements

For the script sample to run correctly:

  1. Open the Control Panel > Administrative Tools > Data Sources (ODBC) window and create a DSN link in the Northwind database.

  2. Use the BDE Administrator utility to create a BDE alias named MYSQL for that database. The alias should use the SQL Server driver.

Note: BDE may fail to recognize some field types of MS SQL Server. For example, it does not support the nvarchar and ntext types. Fields of these types will be unavailable in your tests. To avoid the issue, you can either change field types or work with SQL Server tables by using ADO methods.

JavaScript, JScript

function TestSQL_BDE()
{
  var aTable, S, i;
  // 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; i < aTable.FieldCount; i++)
    S = S + aTable.Field(i).FieldName + "\t";
  S = S + "\r\n";
  // Scan through dataset records
  while ( ! aTable.EOF)
  {
    for (i = 0; i < aTable.FieldCount; i++)
      S = S + aTable.Field(i).AsString + "\t";
    S = S + "\r\n";
    aTable.Next();
  };
  // Output results
  Log.Message("Products", S);
  // Close the table
  aTable.Close();
}

Python

def TestSQL_BDE():
  # 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 in range (0, aTable.FieldCount):
    S = S + aTable.Field[i].FieldName + "\t"
  S = S + "\r\n"
  # Scan through dataset records
  while not aTable.EOF:
    for i in range (0, aTable.FieldCount):
      S = S + aTable.Field[i].AsString + "\t"
    S = S + "\r\n"
    aTable.Next()
  # Output results
  Log.Message("Products", S)
  # Close the table
  aTable.Close()

VBScript

Sub TestSQL_BDE
  ' Create a table
  Set 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
    S = S + aTable.Field(i).FieldName + Chr(9)
  Next
  S = S + Chr(13) + Chr(10)
  ' Scan through dataset records
  Do While Not aTable.EOF
    For i = 0 To aTable.FieldCount - 1
      S = S + aTable.Field(i).AsString + Chr(9)
    Next
    S = S + Chr(13) + Chr(10)
    aTable.Next
  Loop
  ' Output results
  Log.Message "Products", S
  ' Close the table
  aTable.Close
End Sub

DelphiScript

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 aqConvert.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;

C++Script, C#Script

function TestSQL_BDE()
{
  var aTable, S, i;
  // 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; i < aTable["FieldCount"]; i++)
    S = S + aTable["Field"](i)["FieldName"] + "\t";
  S = S + "\r\n";
  // Scan through dataset records
  while ( ! aTable["EOF"])
  {
    for (i = 0; i < aTable["FieldCount"]; i++)
      S = S + aTable["Field"](i)["AsString"] + "\t";
    S = S + "\r\n";
    aTable["Next"]();
  };
  // Output results
  Log["Message"]("Products", S);
  // Close the table
  aTable["Close"]();
}

Highlight search results