移植数据应用程序Porting Data Applications

多年来,Visual C ++ 提供了多种处理数据库的方法。Over the years, Visual C++ has provided several ways to work with databases. 2011 年,Microsoft 宣布将 ODBC 作为从本机代码访问 SQL Server 产品的首选技术。In 2011 Microsoft announced that it is aligning on ODBC as the preferred technology for accessing SQL Server products from native code. ODBC 是一个行业标准,通过它可在多个平台与数据源之间获取代码最佳的可移植性。ODBC is an industry standard, and by using it you gain maximum portability of your code over multiple platforms and data sources. 绝大多数 SQL 数据库和众多 NoSQL 产品都支持 ODBC。Most SQL database products and many NoSQL products support ODBC. 可通过调用低级别 ODBC API、使用 MFC ODBC 包装器类或第三方 C++ 包装器库来直接使用 ODBC。You can use ODBC directly by calling the low-level ODBC APIs, or you can use the MFC ODBC wrapper classes, or a third-party C++ wrapper library.

OLE DB 是基于 COM 规范的低级别、高性能 API,仅在 Windows 上可用。OLE DB is a low-level, high-performance API based on the COM specification, and is only supported on Windows. 如果程序正在访问链接服务器,请使用 OLE DB。Use OLE DB if your program is accessing linked servers. ATL 提供的 OLE DB 模板可简化自定义 OLE DB 提供程序和使用者的创建。ATL provides OLE DB templates that make it easier to create custom OLE DB providers and consumers. 最新版 OLE DB 随附 SQL Native Client 11 一起提供。The most recent version of OLE DB shipped in SQL Native Client 11.

如果旧版应用程序使用 OLE DB 或通过更高级别的 ADO 接口连接到 SQL Server,且你并未访问链接服务器,则应考虑在不久的将来迁移到 ODBC。If your legacy application uses OLE DB or the higher-level ADO interface to connect to SQL Server, and you are not accessing linked servers, you should consider migrating to ODBC in the near future. 如果不需要跨平台可移植性或最新 SQL Server 功能,可使用用于 ODBC 的 Microsoft OLE DB 提供程序 (MSDASQL)。If you do not require cross-platform portability or the latest SQL Server features, you can possibly use the Microsoft OLE DB Provider for ODBC (MSDASQL). MSDASQL 允许在 OLE DB 和 ADO(它在内部使用 OLEDB)上生成的应用程序通过 ODBC 驱动程序访问数据源。MSDASQL allows applications that are built on OLE DB and ADO (which uses OLEDB internally) to access data sources through an ODBC driver. 与转换层一样,MSDASQL 也能影响数据库性能。As with any translation layer, MSDASQL can impact database performace. 请通过测试来判断应用程序的受影响程度。You should test to determine whether the impact is signifant for your application. MSDASQL 附带 Windows 操作系统,而 Windows Server 2008 和 Windows Vista SP1 是首个包含此技术 64 位版本的 Windows 版本。MSDASQL ships with the Windows operating system, and Windows Server 2008 & Windows Vista SP1 are the first Windows releases to include a 64-bit version of the technology.

在单个 DLL 中打包 OLE DB 和 ODBC 驱动程序的 SQL Native Client 组件 (SNAC) 已被 ODBC 应用程序弃用。The SQL Native Client component (SNAC), which packages OLE DB and ODBC drivers in a single DLL, is deprecated for ODBC applications. SNAC 的 SQL Server 2012 版本 (SQLNCLI11.DLL) 随附 SQL Server 2016 一起提供,因为其他 SQL Server 组件都依赖于它。The SQL Server 2012 version of SNAC (SQLNCLI11.DLL) ships with SQL Server 2016 because other SQL Server components depend on it. 但是,通过 ODBC 连接到 SQL Server 或 Azure SQL 数据库的新 C++ 应用程序应使用最新版本的 ODBC 驱动程序However, new C++ applications that connect to SQL Server or Azure SQL Database via ODBC should use the most recent ODBC driver. 有关详细信息,请参阅 SQL Server Native Client Programming(SQL Server Native Client 编程)For more information, see SQL Server Native Client Programming

如果使用的是 C++/CLI,则可以一如既往地使用 ADO.NET。If you use C++/CLI, you can continue to use ADO.NET as always. 有关详细信息,请参阅使用 ADO.NET 进行数据访问 (C++/CLI)在 Visual Studio 中访问数据For more information, see Data Access Using ADO.NET (C++/CLI), and Accessing data in Visual Studio.

  • 除 ODBC 包装器类之外,MFC 还提供数据访问对象 (DAO) 包装器类,用于连接到 Access 数据库。In addition to the ODBC wrapper classes, MFC also provides Data Acess Objects (DAO) wrapper classes for connecting to Access databases. 但 DAO 已过时。However, DAO is obsolete. 应升级任何基于 CDaoDatabase 或 CDaoRecordset 的代码。Any code based on CDaoDatabase or CDaoRecordset should be upgraded.

有关 Microsoft Windows 上数据访问技术历史记录的详细信息,请参阅 Microsoft Data Access Components (Wikipedia)。(Microsoft 数据访问组件(维基百科))For more information about the history of data access technologies on Microsoft Windows, see Microsoft Data Access Components (Wikipedia).

另请参阅See Also

Visual C++ 中的数据访问Data Access in Visual C++
Microsoft Open Database Connectivity (ODBC)(Microsoft 开放式数据库连接 (ODBC))Microsoft Open Database Connectivity (ODBC)
数据访问技术路线图Data Access Technologies Road Map