將 Visual FoxPro 資料庫新增至 SQL Server 為連結的伺服器

本文介紹如何以程序設計方式從 Visual FoxPro 將 Visual FoxPro 數據源新增和查詢為鏈接伺服器。

原始產品版本: Visual FoxPro
原始 KB 編號: 199131

摘要

SQL Server 7.0 和 SQL Server 2000 允許將外部數據源新增為連結的伺服器。 此功能可讓您存取對 OLE DB 數據源散發異質性查詢。 本文說明如何以程序設計方式從 Visual FoxPro 將 Visual FoxPro 數據源新增和查詢為鏈接伺服器。

其他相關資訊

sp_addlinkedserver是 SQL Server 7.0 和 SQL Server 2000 中引進的新預存程式。 sp_addlinkedserver 會建立連結的伺服器,允許對 OLE DB 數據源散發異質性查詢的存取權。

從 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.

下列代碼段會將 SAMPLES\DATA 目錄中的 Visual FoxPro 資料庫Testdata.DBC新增至 SQL Server 為連結的伺服器。 此代碼段假設 Visual FoxPro 和 SQL Server 正在相同的電腦上執行。

注意事項

使用者 Username 必須具有在資料庫上執行這些作業的許可權。

 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

執行代碼段之後,開啟 [SQL Server Enterprise 管理員],然後展開 [鏈接的伺服器] 節點。 已新增名為 VFP 的連結伺服器。

參考資料

如需 的詳細 sp_addlinkedserver資訊,請在 Transact - SQL 參考說明檔中搜尋 sp_addlinkedserver