[Collapse]TestComplete How To
 [Expand]Microsoft Office
 [Expand]Miscellaneous
 [Expand]Network
 [Expand]OpenOffice
 [Collapse]SQL
   Add records to an SQL table/viewarticle/9013/
   Call an SQL Stored Procedure/viewarticle/9014/
   Create an SQL Stored Procedure/viewarticle/9025/
   Create and drop SQL databases/viewarticle/8968/
   Create SQL tables/viewarticle/9007/
   Drop an SQL Stored Procedure/viewarticle/9026/
   Drop SQL tables/viewarticle/9008/
   Files in SQL tables (read and write)/viewarticle/8969/
   Get all databases' names /viewarticle/17636/
   Update records in SQL tables/viewarticle/18441/
 [Expand]TestComplete Utility Scripts
 [Expand]Third-party Controls
 [Expand]Web Utility Scripts
 [Expand]Windows Utility Scripts
Updated: 4/24/2012 Applies To: TestComplete Rating: GoodGoodGoodGoodGood 10 vote(s) Click to rate: PoorNot badAverageGoodExcellent
Add records to an SQL table

The script below adds new records to a table and saves them.

VBScriptCopy Code

Sub Test
  Dim tableName, connectionString
  Dim adOpenKeyset, adLockOptimistic, adCmdTable
  Dim connection, recordset, dataStream
 
  tableName = TABLE_NAME
  connectionString = "Provider=SQLNCLI;Server=SERVER_NAME;"&_
        "Database=DATABASE_NAME;Uid=USER_NAME; Pwd=PASSWORD;"
  
  adOpenKeyset = 1
  adLockOptimistic = 3
  adCmdTable = 2
 
  Set connection = Sys.OleObject("ADODB.Connection")
  connection.ConnectionString = connectionString
  Call connection.Open
  Set recordset = Sys.OleObject("ADODB.Recordset")
  Call recordset.Open(tableName, connection, adOpenKeyset,_
                      adLockOptimistic, adCmdTable)
  Call recordset.AddNew
  recordset.Fields("FIELD0_NAME").Value = "FIELD0_VALUE"
  recordset.Fields("FIELD1_NAME").Value = "FIELD1_VALUE"
  Call recordset.Update
  Call recordset.Close
  Call connection.Close
End Sub

JScriptCopy Code

function UpdateRecordset()
{
  var tableName = TABLE_NAME;
  var connectionString = "Provider=SQLNCLI;Server=SERVER_NAME;" +
          "Database=DATABASE_NAME;Uid=USER_NAME; Pwd=PASSWORD;";
  
  var adOpenKeyset = 1;
  var adLockOptimistic = 3;
  var adCmdTable = 2;
  
  var connection = Sys.OleObject("ADODB.Connection");
  connection.ConnectionString = connectionString;
  connection.Open();
  var recordset = Sys.OleObject("ADODB.Recordset");
  recordset.Open(tableName, connection, adOpenKeyset,
                 adLockOptimistic, adCmdTable);
  recordset.AddNew();
  recordset.Fields("FIELD0_NAME").Value = "FILED0_VALUE";
  recordset.Fields("FIELD1_NAME").Value = "FILED1_VALUE";
  recordset.Update();
  recordset.Close();
  connection.Close();
}


© 2019 SmartBear Software. All rights reserved.
Email Send feedback on this document