f2s_mssql_attach_db

Applies to: MS SQL

Syntax

f2s_mssql_attach_db {database} on {filePath}

Parameter

Description

database

Name of the database

filePath

Path of the MDF file to attach

Description

The f2s_mssql_attach_db command is intended for use with our new support of Microsoft LocalDB’s. When using LocalDB’s a common use case might be to attach to a customer DB, do some work on it, and then detach this DB and attach to a different DB. This was possible using stored procedures, but the code had some complexities that a developer need not worry about. When you attach a database, you must pass the database name, along with the path to an MDF file which contains the database. Any associated LDF’s (log files) and NDF’s (secondary data files) must be in the same folder.

If you call f2s_mssql_attach_db on a database that is already attached, no error will be generated. After you call f2s_mssql_attach_db, you should set the attribute f2s_db_name if you wish to make the attached database the currently selected database.

f2s_mssql_attach_db will work on all supported version of SQL server.

Example

string sDatabase sFile

//login to the local db server instance
login "(localdb)\MyInstance" "" "" "f2s_ms"
//activate the driver
f2s_activate_driver "f2s_ms"

Move "C:\DataFlex Projects\LocalDB\MyDatabase1.mdf" to sFile
Move "MyDatabase1" to sDatabase

//attach the database
f2s_mssql_attach_db sDatabase on sFile
//set the database name
Set_Attribute f2s_db_name of _f2s_ActiveDriverID to sDatabase

//open customer using an int file and find the first record
Open "customer_MSSQL.int" as customer
Clear CUSTOMER
Find ge CUSTOMER by Index.1
Showln (SFormat("***************************  %1  *******************************", sDatabase))
Showln (SFormat("Name: %1; Comments: %2", trim(CUSTOMER.NAME), trim(CUSTOMER.COMMENTS)))
Close CUSTOMER

//detach the database
f2s_mssql_detach_db sDatabase

//attach to another database on the same local db instance
Move "C:\DataFlex Projects\LocalDB\MyDatabase2.mdf" to sFile
Move  "MyDatabase2" to sDatabase

//attach the database
f2s_mssql_attach_db sDatabase on sFile
//set the database name
Set_Attribute f2s_db_name of _f2s_ActiveDriverID to sDatabase

//open customer using an int file and find the first record
Open "customer_MSSQL.int" as customer
Clear CUSTOMER
Find ge CUSTOMER by Index.1
Showln (SFormat("***************************  %1  *******************************", sDatabase))
Showln (SFormat("Name: %1; Comments: %2", trim(CUSTOMER.NAME), trim(CUSTOMER.COMMENTS)))
Close CUSTOMER

//detach the database
f2s_mssql_detach_db sDatabase

//logout
logout "f2s_ms" "(localdb)\MyInstance"

Replaces

SQLSERVER_DETACH_DB