ConnectionString, ConnectionTimeout, DefaultDatabase Properties Example
This example demonstrates the ConnectionString, ConnectionTimeout, and DefaultDatabase properties with a Connection object that is used to execute SQL statements.
Public Sub ConnectionStringX()
Dim strSQLChange As String
Dim strSQLRestore As String
Dim cnn1 As ADODB.Connection
Dim rstTitles As ADODB.Recordset
' Define two SQL statements to execute as command text.
strSQLChange = "UPDATE Titles SET Type = " & _
"'self_help' WHERE Type = 'psychology'"
strSQLRestore = "UPDATE Titles SET Type = " & _
"'psychology' WHERE Type = 'self_help'"
' Open connection.
Set cnn1 = New ADODB.Connection
cnn1.ConnectionString = "driver={SQL Server}" & _
";server=srv;uid=sa;pwd="
cnn1.ConnectionTimeout = 30
cnn1.Open
cnn1.DefaultDatabase = "pubs"
' Execute command text to change the type
' of psychology titles.
cnn1.Execute strSQLChange
' Show the results of executing the command.
Set rstTitles = New ADODB.Recordset
rstTitles.CursorType = adOpenKeyset
rstTitles.LockType = adLockOptimistic
rstTitles.Open "titles", cnn1
With rstTitles
.MoveFirst
Do While Not .EOF
Debug.Print !Title & " - " & !Type
.MoveNext
Loop
End With
' Restore original values because this is a demonstration.
cnn1.Execute strSQLRestore
rstTitles.Close
cnn1.Close
End Sub
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.