Reputation: 50516
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++. I have a username and password, how to connect to it?
Upvotes: 2
Views: 5516
Reputation: 46425
You can connect using ODBC without a DSN using SQLDriverConnect. Specify the name of an installed driver in the connection string. A SQL Server Native client 11 example:
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). The ODBC driver named "SQL Server" ships with windows but a legacy driver provided for backwards compatibility. One should generally use a separately installed ODBC driver, which supports the newer data types and features added since 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).
Upvotes: 8
Reputation: 32703
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. (We use SQL Server authentication, not Windows authentication). To develop/compile such application I need only sqlncli.h and .lib
The SQL Server Native Client OLE DB provider is a low-level COM API that is used for accessing data. The SQL Server Native Client OLE DB provider is recommended for developing tools, utilities, or low-level components that need high performance. 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 Native Client provides OLE DB support to applications connecting to SQL Server.
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).
Have a look a this article When to Use SQL Server Native Client
SQL Server Native Client is one technology that you can use to access data in a SQL Server database. 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.
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.
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. If you don't need access to the new features of SQL Server, you can continue to use Windows Data Access Components (WDAC).
For existing OLE DB and ODBC applications, the primary issue is whether you need to access the new features of SQL Server. If you have a mature application that does not need the new features of SQL Server, you can continue to use WDAC. But if you do need to access those new features, such as the xml data type, you should use 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. (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.
Upvotes: 2