本文介绍如何以编程方式添加和查询 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.
以下代码片段将 VISUAL FoxPro 数据库 Testdata.DBC
从 SAMPLES\DATA 目录作为链接服务器添加到 SQL Server。 此代码片段假定 Visual FoxPro 和 SQL Server 在同一台计算机上运行。
注意
用户 用户名必须有权对数据库执行这些作。
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 Manager 并展开“链接服务器”节点。 添加了名为 VFP 的链接服务器。
参考
有关sp_addlinkedserver
的详细信息,请在 Transact - SQL 参考帮助文件中搜索sp_addlinkedserver
。