简体   繁体   English

如何在没有ODBC的情况下从C ++连接到SQL Server?

[英]How to connect to SQL Server from C++ without ODBC?

I have a SQL Server, all I know about it is that there is no ODBC data source setup on the machine and I need to connect to it from C++. 我有一个SQL Server,我所知道的就是机器上没有ODBC数据源设置,我需要从C ++连接到它。 I have a username and password, how to connect to it? 我有一个用户名和密码,如何连接?

You can connect using ODBC without a DSN using SQLDriverConnect. 您可以使用SQLDriverConnect在没有DSN的情况下使用ODBC进行连接。 Specify the name of an installed driver in the connection string. 在连接字符串中指定已安装驱动程序的名称。 A SQL Server Native client 11 example: 一个SQL Server本机客户端11示例:

Driver={SQL Server Native Client 11.0};Server=SqlHostName;Database=SomeSqlDatabase;UID=YourUserName;PWD=YourPassword

Code snippet: 程式码片段:

retcode = SQLDriverConnect(hdbc
    , NULL
    , (SQLCHAR*)InConnectionString
    , (SQLSMALLINT)sizeof(InConnectionString)
    , (SQLCHAR*)OutConnectionString
    , (SQLSMALLINT)sizeof(OutConnectionString)
    , (SQLSMALLINT*)StringLength2Ptr
    , (SQLUSMALLINT)DriverCompletion
    );

EDIT: 编辑:

The driver name in the connection string must be enclosed in curly braces and exactly match one listed under the Drivers tab of the ODBC Data Source Administrator tool (odbcad32.exe). 连接字符串中的驱动程序名称必须用大括号括起来,并且必须与ODBC数据源管理员工具(odbcad32.exe)的“驱动程序”选项卡下列出的驱动程序名称完全匹配。 The ODBC driver named "SQL Server" ships with windows but a legacy driver provided for backwards compatibility. Windows附带了名为“ SQL Server”的ODBC驱动程序,但提供了旧驱动程序以实现向后兼容。 One should generally use a separately installed ODBC driver, which supports the newer data types and features added since SQL Server 2005. 通常应该使用单独安装的ODBC驱动程序,该驱动程序支持自SQL Server 2005起添加的较新的数据类型和功能。

The latest SQL Server ODBC driver as of this writing is the stand-alone "ODBC Driver 13 for SQL Server" (version 13.1). 撰写本文时,最新的SQL Server ODBC驱动程序是独立的“ SQL Server ODBC驱动程序13”(版本13.1)。

I personally use SQL Server Native Client (OLE DB) in my C++ application to work with SQL Server 2008. And all I need is server IP address, port, username and password to connect to it. 我个人在C ++应用程序中使用SQL Server本机客户端(OLE DB)来与SQL Server 2008一起使用。我所需要的只是连接到它的服务器IP地址,端口,用户名和密码。 (We use SQL Server authentication, not Windows authentication). (我们使用SQL Server身份验证,而不是Windows身份验证)。 To develop/compile such application I need only sqlncli.h and .lib 要开发/编译这样的应用程序,我只需要sqlncli.h和.lib

The SQL Server Native Client OLE DB provider is a low-level COM API that is used for accessing data. SQL Server本机客户端OLE DB提供程序是用于访问数据的低级COM API。 The SQL Server Native Client OLE DB provider is recommended for developing tools, utilities, or low-level components that need high performance. 建议使用SQL Server Native Client OLE DB提供程序来开发需要高性能的工具,实用程序或低级组件。 The SQL Server Native Client OLE DB provider is a native, high performance provider that accesses the SQL Server Tabular Data Stream (TDS) protocol directly. SQL Server本机客户端OLE DB提供程序是一种本机高性能提供程序,可直接访问SQL Server表格数据流(TDS)协议。

SQL Server Native Client provides OLE DB support to applications connecting to SQL Server. SQL Server本机客户端为连接到SQL Server的应用程序提供OLE DB支持。

It is low level, but gives access to all features of the server. 它是低级的,但是可以访问服务器的所有功能。 For example, I call stored procedures with table-valued parameters and I use types like varbinary(max). 例如,我使用表值参数调用存储过程,并且使用诸如varbinary(max)之类的类型。

Have a look a this article When to Use SQL Server Native Client 看看本文何时使用SQL Server Native Client

SQL Server Native Client is one technology that you can use to access data in a SQL Server database. SQL Server Native Client是一种可用于访问SQL Server数据库中的数据的技术。 For a discussion of the different data-access technologies, see Data Access Technologies Road Map 有关不同数据访问技术的讨论,请参阅数据访问技术路线图

When deciding whether to use SQL Server Native Client as the data access technology of your application, you should consider several factors. 在决定是否使用SQL Server Native Client作为应用程序的数据访问技术时,应考虑几个因素。

For new applications, if you're using a managed programming language such as Microsoft Visual C# or Visual Basic, and you need to access the new features in SQL Server, you should use the .NET Framework Data Provider for SQL Server, which is part of the .NET Framework. 对于新应用程序,如果使用的是Microsoft Visual C#或Visual Basic之类的托管编程语言,并且需要访问SQL Server中的新功能,则应使用SQL Server的.NET Framework数据提供程序。 .NET Framework。

If you are developing a COM-based application and need to access the new features introduced in SQL Server, you should use SQL Server Native Client. 如果要开发基于COM的应用程序,并且需要访问SQL Server中引入的新功能,则应使用SQL Server Native Client。 If you don't need access to the new features of SQL Server, you can continue to use Windows Data Access Components (WDAC). 如果不需要访问SQL Server的新功能,则可以继续使用Windows数据访问组件(WDAC)。

For existing OLE DB and ODBC applications, the primary issue is whether you need to access the new features of SQL Server. 对于现有的OLE DB和ODBC应用程序,主要问题是是否需要访问SQL Server的新功能。 If you have a mature application that does not need the new features of SQL Server, you can continue to use WDAC. 如果您具有不需要SQL Server新功能的成熟应用程序,则可以继续使用WDAC。 But if you do need to access those new features, such as the xml data type, you should use SQL Server Native Client. 但是,如果确实需要访问这些新功能(例如xml数据类型),则应使用SQL Server Native Client。

Both SQL Server Native Client and MDAC support read committed transaction isolation using row versioning, but only SQL Server Native Client supports snapshot transaction isolation. SQL Server Native Client和MDAC都支持使用行版本控制读取提交的事务隔离,但是只有SQL Server Native Client支持快照事务隔离。 (In programming terms, read committed transaction isolation with row versioning is the same as Read-Committed transaction.) (在编程方面,具有行版本控制的读取提交的事务隔离与读取提交的事务相同。)

For information about the differences between SQL Server Native Client and MDAC, see Updating an Application to SQL Server Native Client from MDAC. 有关SQL Server Native Client和MDAC之间的区别的信息,请参阅从MDAC将应用程序更新到SQL Server Native Client。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM