Numéro d'article: 207595 - Dernière mise à jour: mercredi 4 mars 2009 - Version: 6.0

Comment faire de SQL Server 2000 des requêtes distribuées avec les fichiers .dbf de FoxPro

A noterCet article s'applique à un système d'exploitation différent de celui que vous utilisez. Le contenu de l'article qui ne vous concerne peut-être pas est désactivé.
Agrandir tout | Réduire tout

Résumé

Cet article montre comment effectuer une requête distribuée de SQL Server pour récupérer des données à partir de fichiers .dbc et .dbf FoxPro à l'aide du pilote ODBC VFP ou le fournisseur OLE DB VFP.

Plus d'informations

Microsoft SQL Server 2000 fournit la possibilité d'effectuer des requêtes sur des fournisseurs OLE DB. Cela en utilisant les fonctions OpenQuery ou OpenRowset de Transact-SQL ou à l'aide d'une requête avec des noms en quatre parties incluant un nom de serveur lié.

Par exemple :

sp_addlinkedserver 'mylinkedserver', 'nom_de_produit', 'myoledbprovider', 'source_de_données', 'location', 'chaîne_fournisseur', 'catalogue'

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

Vous devez utiliser le fournisseur de Microsoft OLE DB pour ODBC (MSDASQL) et le pilote Visual FoxPro ODBC pour configurer un serveur lié pour exécuter des requêtes distribuées sur les fichiers .dbc et .dbf FoxPro. L'utilisation du fournisseur Jet OLEDB avec FoxPro n'est pas prise en charge. Le pilote VFP ODBC n'est pas thread-safe. Dans la mesure où SQL Server est multithread, le pilote ODBC VFP peut provoquer des problèmes dans certaines circonstances. S'il est possible, nous vous recommandons d'utiliser le fournisseur OLE DB VFP pour vous connecter aux données SQL Server.

L'exemple de code T-SQL suivant montre comment configurer et utiliser des requêtes distribuées avec FoxPro avec fonctions OpenQuery et OpenRowset. Il montre également comment mettre à jour une table FoxPro à distance à partir de SQL Server 2000. Vous pouvez tester ce code dans l'Analyseur de requêtes SQL après avoir installé le pilote Visual FoxPro ODBC sur un ordinateur SQL Server 2000. Vous devez modifier les noms de source de données et chemin d'accès à la FoxPro fichiers selon le cas :
/* OPENROWSET and OPENQUERY examples with VFP via ODBC OLE DB provider */ 

/* These OPENROWSET examples depend on the sample files VFP98\data\Testdata.dbc
Modify your code accordingly for differences in location or DBC name */ 

--====================================================
-- Using DBC file , read and update
--====================================================
-- OPENROWSET DSN-less example

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 example
/* Note the DSN Example might fail if SQL Server is configured to use a local account.*/ 
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 examples */ 
-- sp_addlinkedserver example with DSN

/* You will need to make a DSN and point it to the Testdata database. 
Modify your code accordingly for differences in location or DBC name */ 

/* Note this Example may fail if SQL Server is configured to use a local account.*/ 
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

--====================================================
-- Using 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<BR/>

-- 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's QUOTED_IDENTIFIER has to be set to OFF.

 

SET QUOTED_IDENTIFIER OFF

 

            -- OPENROWSET DSN-less example

            

            select * from openrowset('MSDASQL',

            'Driver=Microsoft Visual FoxPro Driver;

            SourceDB=e:\VFP90\samples\data\Testdata.dbc;

            SourceType=DBC',

            'select * from customer where country = "USA" order by city')

            go

            

            select * from  openrowset('MSDASQL',

            'Driver=Microsoft Visual FoxPro Driver;

            SourceDB=e:\VFP90\samples\data\Testdata.dbc;

            SourceType=DBC',

            'select * from customer where region="WA"')

            go

            

            Update  openrowset('MSDASQL',

            'Driver=Microsoft Visual FoxPro Driver;

            SourceDB=e:\VFP90\samples\data\Testdata.dbc;

            SourceType=DBC',

            'select * from customer where city = "Seattle"')

            set region = "WW" 

            go

            

            -- check to verify which rows were updated

            select * from  openrowset('MSDASQL',

            'Driver=Microsoft Visual FoxPro Driver;

            SourceDB=e:\VFP90\samples\data\Testdata.dbc;

            SourceType=DBC',

            'select * from customer where region="WW"') 

            go

            

            -- OPENROWSET DSN example

            /* Note the DSN Example might fail if SQL Server is configured to use a local account.*/ 

            select * from openrowset('MSDASQL',

            'DSN=Visual FoxPro Database;

            SourceDB=e:\VFP90\samples\data\Testdata.dbc;

            SourceType=DBC',

            'select * from customer where country = "USA" order by city')

            go

            

            /* sp_addlinkedserver examples */ 

            -- sp_addlinkedserver example with DSN

            

            /* You will need to make a DSN and point it to the Testdata database. 

            Modify your code accordingly for differences in location or DBC name */ 

            

            /* Note this Example may fail if SQL Server is configured to use a local account.*/ 

            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 city = "Seattle" ') 

            go

            

            -- We will set the region back to "WA" if it currently is "WW".

            -- Update using OpenQuery

            Update OPENQUERY([VFP Testdata Database With DSN], 'select * from customer where city = "Seattle" ') 

            set region = "WA" 

            go

            

            -- Make sure that the region got updated.

            SELECT *

            FROM OPENQUERY([VFP Testdata Database With DSN], 'select * from customer where city = "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:\VFP90\samples\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 city') 

            go

            

            --====================================================

            -- Using 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:\VFP90\samples\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:\VFP90\samples\data;

            SourceType=DBF',

            'select * from customer where city = "Seattle"')

            set region = "WW" 

            go

            

            -- verify update

            

            select * from openrowset('MSDASQL',

            'Driver=Microsoft Visual FoxPro Driver;

            SourceDB=e:\VFP90\samples\data;

            SourceType=DBF',

            'select * from customer where region = "WW"')

            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:\VFP90\samples\data;

            SourceType=DBF',

            'select * from customer where country != "USA" order by country') 

            go

vous pouvez également utiliser le fournisseur Visual FoxPro OLE DB pour créer une requête distribuée. Il s'agit de la technologie préférée à utiliser. Notez que pendant que ce code montre comment mettre à jour et supprimer des données, ajout, la mise à jour (modifiez), et supprimer des données dans une requête distribuée à l'aide du fournisseur OLE DB n'est pas prise en charge.

L'exemple de code T-SQL suivant montre comment configurer et utiliser la requête distribuée avec FoxPro avec fonctions OpenQuery et OpenRowset. Vous pouvez tester ce code dans l'Analyseur de requêtes SQL après avoir installé le fournisseur Visual FoxPro OLE DB sur une machine SQL Server 2000. Vous devez modifier les noms de source de données et chemin d'accès à la FoxPro fichiers selon le cas :
 '/* These OPENROWSET examples depend on the sample files VFP98\data\Testdata.dbc

'Modify your code accordingly for differences in location or DBC name */

 

--*====================================================

--* Using the DBC file, reading and updating data.

--*====================================================

--* A couple of OPENROWSET queries.

select * from openrowset('VFPOLEDB',

'e:\vfp7junk\Testdata.dbc';'Exclusive=No';'Data Source=DBC',

'select * from customer where country != "USA" order by country')

go

 

Select * from openrowset('VFPOLEDB',

'e:\vfp7junk\Testdata.dbc';'Exclusive=No';'Data Source=DBC',

'select * from customer where region="WA"')

go

 

--* Need to use an error trapping routine with the UPDATE and DELETE functions:

select * from

 openrowset('VFPOLEDB',

   'E:\VFP7Junk\Testdata.DBC';'Exclusive=No';'Data Source=DBC',

   'Update Customer Set city = "SEATTLE" where region = "WA" ') 

go

declare @upderror int

select @upderror = @@error

print ''

if @upderror != 7357 and @upderror != 0

            print  'Update failed with error '+convert(varchar(5),@upderror)

else

            print 'Ignore the error above, the Update succeeded'

go

 

 

-- check to verify which rows were updated

select * from  openrowset('VFPOLEDB',

'E:\VFP7junk\Testdata.DBC';'Exclusive=No';'Data Source=DBC',

'select * from customer where region = "WA"')

go

 

--* Change the City field back to "Seattle".

 

select * from

 openrowset('VFPOLEDB',

   'E:\VFP7Junk\Testdata.DBC';'Exclusive=No';'Data Source=DBC',

   'Update Customer Set city = "Seattle" where region = "WA" ') 

go

declare @upderror int

select @upderror = @@error

print ''

if @upderror != 7357 and @upderror != 0

            print  'Update failed with error '+convert(varchar(5),@upderror)

else

            print 'Ignore the error above, the Update succeeded'

go

 

--* The DELETE fucntion also causes an error, but the DELETE works.

select * from

 openrowset('VFPOLEDB',

   'E:\VFP7Junk\Testdata.DBC';'Exclusive=No';'Data Source=DBC',

   'Delete from Customer where country = "Spain" ') 

go

declare @delerror int

select @delerror = @@error

print ''

if @delerror != 7357 and @delerror != 0

            print  'Delete failed with error '+convert(varchar(5),@delerror)

else

            print 'Ignore the error above, the Delete succeeded'

go

 

--* Check to see that the records are deleted.

Select * from openrowset('VFPOLEDB',

'e:\vfp7junk\Testdata.dbc';'Exclusive=No';'Data Source=DBC',

'select * from customer where country = "Spain"')

go

 

 

--* Here are some examples using the VFP OLE DB Provider to create Linked Servers.

--* Using sp_addlinkedserver to create the Linked Server.

sp_addlinkedserver @server='VFP_Linked_Server',

@srvproduct='Microsoft Visual FoxPro OLE DB Provider', 

@provider='VFPOLEDB',

@datasrc = 'E:\vfp7junk'

go

 

SELECT *

FROM OPENQUERY([VFP_Linked_Server], 'select * from customer where city = "Seattle"')

go

 

 

-- The Update command will update the table with the OPENQUERY function when using the 

-- linked server, but the same error 7357 error will occur.

select * from

 OPENQUERY([VFP_Linked_Server],

   'Update Customer Set city = "SEATTLE" where region = "WA" ') 

go

declare @upderror int

select @upderror = @@error

print ''

if @upderror != 7357 and @upderror != 0

            print  'Update failed with error '+convert(varchar(5),@upderror)

else

            print 'Ignore the error above, the Update succeeded'

go

 

 

-- Check and see if the City field is all uppercase with "SEATTLE".

SELECT *

FROM OPENQUERY([VFP_Linked_Server], 'select * from customer where region = "WA"')

go

 

--* Let's check for how many records have the word "London" in the City field.

SELECT *

FROM OPENQUERY([VFP_Linked_Server], 'select * from customer where city = "London"')

go

 

-- We can also use the Delete command to remove records with the OPENQUERY function when using the 

-- linked server, but the same error 7357 error will occur.

select * from

 OPENQUERY([VFP_Linked_Server],

   'Delete from Customer where city = "London"') 

go

declare @delerror int

select @delerror = @@error

print ''

if @delerror != 7357 and @delerror != 0

            print  'Delete failed with error '+convert(varchar(5),@delerror)

else

            print 'Ignore the error above, the Delete succeeded'

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:\VFP8junk\Testdata.dbc;SourceType=DBC;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;'

go

 

 

SELECT *

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

go

Références

Pour plus d'informations sur la configuration et à l'aide de requêtes distribuées, examinons sp_addlinkedserver, OpenQuery, OpenRowset et des rubriques connexes dans la documentation en ligne de SQL 7.0.

Pour en savoir plus sur FoxPro et les fichiers .dbf ou .dbc, reportez-vous à la documentation de produit FoxPro.

Les informations contenues dans cet article s'appliquent au(x) produit(s) suivant(s):
  • Microsoft Visual FoxPro 3.0 Édition Standard
  • Microsoft Visual FoxPro 3.0b Édition standard
  • Microsoft Visual FoxPro 5.0 Édition standard
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Édition Professionnelle
  • Microsoft Visual FoxPro 7.0 Édition professionnelle
  • Microsoft Visual FoxPro 8.0 Édition Professionnelle
  • Microsoft Visual FoxPro 9.0 Professional Edition
  • Microsoft SQL Server 2000 Standard
Mots-clés : 
kbmt kbdatabase kbhowto KB207595 KbMtfr
Traduction automatiqueTraduction automatique
IMPORTANT : Cet article est issu du système de traduction automatique mis au point par Microsoft (http://support.microsoft.com/gp/mtdetails). Un certain nombre d?articles obtenus par traduction automatique sont en effet mis à votre disposition en complément des articles traduits en langue française par des traducteurs professionnels. Cela vous permet d?avoir accès, dans votre propre langue, à l?ensemble des articles de la base de connaissances rédigés originellement en langue anglaise. Les articles traduits automatiquement ne sont pas toujours parfaits et peuvent comporter des erreurs de vocabulaire, de syntaxe ou de grammaire (probablement semblables aux erreurs que ferait une personne étrangère s?exprimant dans votre langue !). Néanmoins, mis à part ces imperfections, ces articles devraient suffire à vous orienter et à vous aider à résoudre votre problème. Microsoft s?efforce aussi continuellement de faire évoluer son système de traduction automatique.
La version anglaise de cet article est la suivante: 207595  (http://support.microsoft.com/kb/207595/en-us/ )
L'INFORMATION CONTENUE DANS CE DOCUMENT EST FOURNIE PAR MICROSOFT SANS GARANTIE D'AUCUNE SORTE, EXPLICITE OU IMPLICITE. L'UTILISATEUR ASSUME LE RISQUE DE L'UTILISATION DU CONTENU DE CE DOCUMENT. CE DOCUMENT NE PEUT ETRE REVENDU OU CEDE EN ECHANGE D'UN QUELCONQUE PROFIT.