Aggiungere un database Visual FoxPro a SQL Server come server collegato

Questo articolo illustra come aggiungere ed eseguire query a livello di codice su un'origine dati Visual FoxPro come server collegato da Visual FoxPro.

Versione originale del prodotto: Visual FoxPro
Numero KB originale: 199131

Riepilogo

SQL Server 7.0 e SQL Server 2000 consentono l'aggiunta di origini dati esterne come server collegati. Questa funzionalità consente di accedere a query distribuite ed eterogenee su origini dati OLE DB. Questo articolo illustra come aggiungere ed eseguire query a livello di codice su un'origine dati Visual FoxPro come server collegato da Visual FoxPro.

Ulteriori informazioni

sp_addlinkedserver è una nuova stored procedure introdotta in SQL Server 7.0 e SQL Server 2000. sp_addlinkedserver crea un server collegato, che consente l'accesso a query eterogenee e distribuite su origini dati OLE DB.

La sintassi per l'aggiunta di un server collegato da Transact - SQL è:

 sp_addlinkedserver [@server =] 'server',
 [@srvproduct =] 'product_name',
 [@provider =] 'provider_name',
 [@datasrc =] 'data_source',
 [@location =] 'location',
 [@provstr =] 'provider_string',
 [@catalog =] 'catalog'[@server =] 'server' Is the name of the linked server to
 create with sp_addlinkedserver.

[@srvproduct =] 'product_name' Is the product name of the OLE DB data
 source to add as a linked server.

[@provider =] 'provider_name' Is the unique provider identifier of the
 OLE DB provider corresponding to the
 data source.

[@datasrc =] 'data_source' Is the name of the data source, as
 interpreted by the OLE DB provider.

[@location =] 'location' Is the location of or path to the
 database as interpreted by the OLE DB
 provider.

[@provstr =] 'provider_string' Is the OLE DB provider-specific.

[@catalog =] 'catalog' Is the catalog to be used when making a
 connection to the OLE DB provider.

Il frammento di codice seguente aggiunge il database Testdata.DBC Visual FoxPro dalla directory SAMPLES\DATA a SQL Server come server collegato. Questo frammento di codice presuppone che Visual FoxPro e SQL Server vengano eseguiti nello stesso computer.

Nota

L'utente, Username, deve disporre delle autorizzazioni necessarie per eseguire queste operazioni nel database.

 Source_Path=IIF(VAL(SUBSTR(VERSION(),15,2))=6,HOME(2),HOME()+"SAMPLES\")
 Connect_String='DRIVER={SQL Server};' + ;
 'SERVER=MY_SERVER;DATABASE=PUBS;UID=UserName;PWD=StrongPassword'
 gnConnHandle=SQLSTRINGCONN(Connect_String)
 IF gnConnHandle > 0
 * Create a command string to pass to SQL Server via SQLExec
 SQLCommand="sp_addlinkedserver 'VFP','','MSDASQL',NULL,NULL,"+ ;
 "'DRIVER={Microsoft Visual FoxPro Driver};" + ;
 "SourceDB="+Source_Path+"DATA\TESTDATA.DBC;SourceType=DBC;NULL'"* CREATE the LINKED Server"
 Create_Linked_Server=SQLExec(gnConnHandle,SQLCommand)
 IF Create_Linked_Server > 0
 * The linked server was successfully created
 * Run the query
 =RunQuery()
 ELSE
 * The Linked Server either already exists or the command failed.
 * Test for existence of linked server with aerror()
 =AERROR(s_failed)
 IF "VFP' ALREADY EXISTS."$UPPER(s_failed[1,2])
 * The linked server exists, so run the query
 =RunQuery()
 ELSE
 * The linked server doesn't exist, so display a message
 =MESSAGEBOX(s_failed[1,2],32,'Failed')
 ENDIF
 ENDIF
 =SQLDISCONN(gnConnHandle)
 ENDIF

PROCEDURE RunQuery
 SQLCommand="SELECT * FROM OPENQUERY(VFP,'SELECT * FROM CUSTOMER')"
 QRYVal=SQLExec(gnConnHandle,SQLCommand,'SQLRESULTS')
 IF QRYVal > 0
 SELECT SQLResults
 BROW
 ELSE
 =AERROR(L_Server)
 =MESSAGEBOX(L_Server[1,2],32,'Query Failed')
 ENDIF
 RETURN

Dopo aver eseguito il frammento di codice, aprire SQL Server Enterprise Manager ed espandere il nodo Server collegati. È stato aggiunto un server collegato denominato VFP.

Riferimenti

Per altre informazioni su sp_addlinkedserver, cercare sp_addlinkedserver nel file della Guida di riferimento Transact - SQL.