Xbasic

SQL::ConnectionOpen Method

Syntax

Result_Flag as L = Open([SQLConnectionString as C [, UserName as C [, Password as C]]])

Arguments

SQLConnectionStringCharacter

See SQL::Connection::SetConnectionString().

UserNameCharacter

Character

PasswordCharacter

Character

Returns

Result_FlagLogical

TRUE (.T.) if the operation was successful; otherwise FALSE (.F.).

Description

Connect to a database. (See help for SetConnectionString for details on constructing a connect string.)

Discussion

The Open() method connects to a database. The Connect_String can take two forms. The following version explicitly connects to the AlphaSports.mdb file.

dim conn as SQL::Connection
conn.open("{A5API='Access', FileName='c:\program files\a5v8\mdbfiles\alphasports.mdb'}")

If you have saved this connection string information under the name "AlphaSports", this syntax would work equally well. This syntax is applicable to the SQL::Connection::Open() method only.

dim conn as SQL::Connection
conn.open("::name::alphasports")

Another possible syntax allows you to resolve the connection string from an expression. The syntax is: ::eval:: String Expression. String Expression must evaluate to a valid connection string. For example: ::Eval::MyFunction().

dim c as sql::connection
if c.open("::eval::LookupConnection(\"Northwind\")")
    ResultString = "Connection opened Successfully"
    ResultString = ResultString + crlf(2) + "Tables" + crlf(2) + c.ListTables()
else
    ResultString = "Error: " + c.callresult.text
end if
a5_show_variable(ResultString, "Open Result")
' --------------------------------------------------------------------------------------
function LookupConnection as C (Name as C)
    if Name = "northwind"
        LookupConnection = "{A5API=Access,FileName='c:\northwind.mdb',UserName='Admin'}"
    else
        LookupConnection = "{A5API=Access,FileName='c:\common.mdb',UserName='Admin'}"
    end if
end function

Example

dim conn as SQL::Connection
dim connString as C
connString = "{A5API='Access', FileName='c:\program files\a5v8\mdbfiles\alphasports.mdb'}"
if .not. conn.open(connString)
    ui_msg_box("Error", conn.CallResult.text)
    end
end if
... do stuff
conn.close()

See Also