频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

如何用VFP的dbf进行SQL Server 7.0 的分布式查询

来源: 作者: 出处:巧巧读书 2006-10-27 进入讨论组
谢 谢 收 藏 http://www.qqread.com/foxpro/s255309.html


概述

本文演示如何执行一个 SQL Server 分布式查询来从 FoxPro .dbc 和 .dbf 文件获取数据.

更多信息

Microsoft SQL Server 7.0 提供了执行基于 OLE DB providers 的查询的能力. 这是靠使用 OpenQuery 或 OpenRowset Transact-SQL 函数或使用包含一个已连接服务器名四部查询来执行的.

例如:

sp_addlinkedserver 'mylinkedserver', 'product_name', 'myoledbprovider', 'data_source','location', 'provider_string', 'catalog'

SELECT * FROM OPENQUERY(mylinkedserver, 'select * from table1')

可以用 Microsoft OLE DB provider for ODBC (MSDASQL) 和 Visual FoxPro ODBC driver 来设置一个已连接服务器 (linked server) 来执行基于 FoxPro .dbc 和 .dbf 文件的分布式查询. 不支持用 Jet OLEDB Provider 和 FoxPro.

以下 T-SQL 示例代码演示如何用 OpenQuery 和 OpenRowset 函数设置和使用分布式查询 FoxPro. 它也演示了如何从 SQL Server 更新一个远程 FoxPro 表. 你可以在一个 SQL Server 7.0 机器上安装了 Visual FoxPro ODBC driver 后, 在 SQL Query Analyzer 中测试这些代码 . 你需要改变数据源名和路径到适当的 FoxPro 文件:


/* OPENROWSET 和 OPENQUERY 示例. 以 VFP 通过 ODBC OLE DB provider */

/* 这些 OPENROWSET 示例依赖于示例文件 VFP98\data\Testdata.dbc
如果你的数据在不同的位置请修改你的代码 */

--====================================================
-- 使用 DBC 文件 , 读和更新
--====================================================
-- OPENROWSET DSN-less 示例

select * from openrowset('MSDASQL',
'Driver=Microsoft Visual FoxPro Driver;
SourceDB=e:\VFP98\data\Testdata.dbc;
SourceType=DBC',
'select * from customer where country != "USA" order by country')
go

select * from openrowset('MSDASQL',
'Driver=Microsoft Visual FoxPro Driver;
SourceDB=e:\VFP98\data\Testdata.dbc;
SourceType=DBC',
'select * from customer where region="WA"')
go

Update openrowset('MSDASQL',
'Driver=Microsoft Visual FoxPro Driver;
SourceDB=e:\VFP98\data\Testdata.dbc;
SourceType=DBC',
'select * from customer where region="WA"')
set region = "Seattle"
go

-- check to verify which rows were updated
select * from openrowset('MSDASQL',
'Driver=Microsoft Visual FoxPro Driver;
SourceDB=e:\VFP98\data\Testdata.dbc;
SourceType=DBC',
'select * from customer where region="Seattle"')
go

-- OPENROWSET DSN 示例
/* 注意:如果 SQL Server 是配置为使用本地帐号 DSN 示例可能失败.*/
select * from openrowset('MSDASQL',
'DSN=Visual FoxPro Database;
SourceDB=e:\VFP98\data\Testdata.dbc;
SourceType=DBC',
'select * from customer where country != "USA" order by country')
go

/* sp_addlinkedserver 示例 */
-- sp_addlinkedserver example with DSN

/* 你需要生成一个 DSN 并使它指向 Testdata 数据库.
修改你的代码以反映 DBC 位置 */

/* 注意:如果 SQL Server 是配置为使用本地帐号 DSN 示例可能失败.*/
sp_addlinkedserver 'VFP Testdata Database With DSN',
'',
'MSDASQL',
'VFP System DSN'
go

sp_addlinkedsrvlogin 'VFP Testdata Database With DSN', FALSE, NULL, NULL, NULL
go

SELECT *
FROM OPENQUERY([VFP Testdata Database With DSN], 'select * from customer where region = "Seattle" ')
go

-- Update using OpenQuery
Update OPENQUERY([VFP Testdata Database With DSN], 'select * from customer where region="WA"')
set region = "Seattle"
go

/* SP_addlinkedserver example with DSN-less connection */

/* This example also depends on the sample files Testdata.dbc
Modify your code accordingly for differences in location or DBC name */

sp_addlinkedserver 'VFP Testdata Database With No DSN',
'',
'MSDASQL',
NULL,
NULL,
'Driver={Microsoft Visual FoxPro Driver};UID=;PWD=;SourceDB=e:\VFP98\data\Testdata.dbc;SourceType=DBC;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;'
go

sp_addlinkedsrvlogin 'VFP Testdata Database With No DSN', FALSE, NULL, NULL, NULL
go

SELECT *
FROM OPENQUERY([VFP Testdata Database With No DSN], 'select * from customer where country != "USA" order by country')
go

--====================================================
-- 使用 VFP 6.0 driver, read and update data from VFP sample dbf files
--====================================================

-- OPENROWSET DSN-less example

select * from openrowset('MSDASQL',
'Driver=Microsoft Visual FoxPro Driver;
SourceDB=e:\VFP98\data;
SourceType=DBF',
'select * from customer where country != "USA" order by country')
go

-- perform UPDATE

Update openrowset('MSDASQL',
'Driver=Microsoft Visual FoxPro Driver;
SourceDB=e:\VFP98\data;
SourceType=DBF',
'select * from customer where region="Seattle"')
set region = "WA"
go

-- verify update

select * from openrowset('MSDASQL',
'Driver=Microsoft Visual FoxPro Driver;
SourceDB=e:\VFP98\data;
SourceType=DBF',
'select * from customer where region = "WA"')
go


-- OPENROWSET DSN example
-- DSN points to the folder where .dbf files are.
/* Note this Example may fail if SQL Server is configured to use a local account.*/
select * from openrowset('MSDASQL',
'DSN=Visual FoxPro Tables;
SourceDB=e:\VFP98\data;
SourceType=DBF',
'select * from customer where country != "USA" order by country')
go

更多文章 更多内容请看SQL Server 数据处理专题SQL Server 索引和查询专题SQL Server专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章