Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - Programming » SQL Anywhere OLE DB and ADO APIs » ADO programming with SQL Anywhere

Connecting to a database with the Connection object Next Page

Executing statements with the Command object

This section describes a simple routine that sends a simple SQL statement to the database.

Sample code

You can try this routine by placing a command button named Command2 on a form, and pasting the routine into its Click event. Run the program and click the button to connect, display a message in the Server Messages window, and then disconnect.

Private Sub cmdUpdate_Click()
    ' Declare variables
    Dim myConn As New ADODB.Connection
    Dim myCommand As New ADODB.Command
    Dim cAffected As Long
    ' Establish the connection
    myConn.Provider = "SAOLEDB"
    myConn.ConnectionString = _
      "Data Source=SQL Anywhere 10 Demo"

    'Execute a command
    myCommand.CommandText = _
    "update Customers set GivenName='Liz' where ID=102"
    Set myCommand.ActiveConnection = myConn
    myCommand.Execute cAffected
    MsgBox CStr(cAffected) +
 " rows affected.", vbInformation

End Sub

After establishing a connection, the example code creates a Command object, sets its CommandText property to an update statement, and sets its ActiveConnection property to the current connection. It then executes the update statement and displays the number of rows affected by the update in a message box.

In this example, the update is sent to the database and committed as soon as it is executed.

For information on using transactions within ADO, see Using transactions.

You can also perform updates through a cursor.

For more information, see Updating data through a cursor.