Skip to content

Latest commit

 

History

History
169 lines (129 loc) · 9.55 KB

using-ado-with-sql-server-native-client.md

File metadata and controls

169 lines (129 loc) · 9.55 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic helpviewer_keywords
Using ADO
To use new features introduced in SQL Server 2005, existing applications that use ADO should use the SQL Server Native Client OLE DB provider.
markingmyname
maghan
03/16/2017
sql
native-client
reference
SQL Server Native Client, ADO
data access [SQL Server Native Client], ADO
ADO [SQL Server Native Client]
SQLNCLI, ADO

Using ADO with SQL Server Native Client

[!INCLUDE SQL Server]

Important

[!INCLUDEsnac-removed-oledb-and-odbc]

In order to take advantage of new features introduced in [!INCLUDEssVersion2005] such as multiple active result sets (MARS), query notifications, user-defined types (UDTs), or the new xml data type, existing applications that use ActiveX Data Objects (ADO) should use the [!INCLUDEssNoVersion] Native Client OLE DB provider as their data access provider.

If you do not need to use any of the new features introduced in [!INCLUDEssVersion2005], there is no need to use the [!INCLUDEssNoVersion] Native Client OLE DB provider; you can continue using your current data access provider, which is typically SQLOLEDB. If you are enhancing an existing application and you need to use the new features introduced in [!INCLUDEssVersion2005], you should use [!INCLUDEssNoVersion] Native Client OLE DB provider.

Note

If you are developing a new application, it is recommended that you consider using ADO.NET and the .NET Framework Data Provider for [!INCLUDEssNoVersion] instead of [!INCLUDEssNoVersion] Native Client to access all the new features of recent versions of [!INCLUDEssNoVersion]. For more information about the .NET Framework Data Provider for [!INCLUDEssNoVersion], see the .NET Framework SDK documentation for ADO.NET.

To enable ADO to use new features of recent versions of [!INCLUDEssNoVersion], some enhancements have been made to the [!INCLUDEssNoVersion] Native Client OLE DB provider which extends the core features of OLE DB. These enhancements allow ADO applications to use newer [!INCLUDEssNoVersion] features and to consume two data types introduced in [!INCLUDEssVersion2005]: xml and udt. These enhancements also exploit enhancements to the varchar, nvarchar, and varbinary data types. [!INCLUDEssNoVersion] Native Client adds the SSPROP_INIT_DATATYPECOMPATIBILITY initialization property to the DBPROPSET_SQLSERVERDBINIT property set for use by ADO applications so that the new data types are exposed in a way compatible with ADO. In addition, the [!INCLUDEssNoVersion] Native Client OLE DB provider also defines a new connection string keyword named DataTypeCompatibility that is set in the connection string.

Note

Existing ADO applications can access and update XML, UDT, and large value text and binary field values using the SQLOLEDB provider. The new larger varchar(max), nvarchar(max), and varbinary(max) data types are returned as the ADO types adLongVarChar, adLongVarWChar and adLongVarBinary respectively. XML columns are returned as adLongVarChar, and UDT columns are returned as adVarBinary. However, if you use the [!INCLUDEssNoVersion] Native Client OLE DB provider (SQLNCLI11) instead of SQLOLEDB, you need to make sure to set the DataTypeCompatibility keyword to "80" so that the new data types will map correctly to the ADO data types.

Enabling SQL Server Native Client from ADO

To enable the usage of [!INCLUDEssNoVersion] Native Client, ADO applications will need to implement the following keywords in their connection strings:

  • Provider=SQLNCLI11

  • DataTypeCompatibility=80

For more information about the ADO connections string keywords supported in [!INCLUDEssNoVersion] Native Client, see Using Connection String Keywords with SQL Server Native Client.

The following is an example of establishing an ADO connection string that is fully enabled to work with [!INCLUDEssNoVersion] Native Client, including the enabling of the MARS feature:

Dim con As New ADODB.Connection  
  
con.ConnectionString = "Provider=SQLNCLI11;" _  
         & "Server=(local);" _  
         & "Database=AdventureWorks;" _   
         & "Integrated Security=SSPI;" _  
         & "DataTypeCompatibility=80;" _  
         & "MARS Connection=True;"  
con.Open  

Examples

The following sections provide examples of how you can use ADO with the [!INCLUDEssNoVersion] Native Client OLE DB provider.

Retrieving XML Column Data

In this example, a recordset is used to retrieve and display the data from an XML column in the [!INCLUDEssNoVersion] AdventureWorks sample database.

Dim con As New ADODB.Connection  
Dim rst As New ADODB.Recordset  
Dim sXMLResult As String  
  
con.ConnectionString = "Provider=SQLNCLI11;" _  
         & "Server=(local);" _  
         & "Database=AdventureWorks;" _   
         & "Integrated Security=SSPI;" _   
         & "DataTypeCompatibility=80;"  
  
con.Open  
  
' Get the xml data as a recordset.  
Set rst.ActiveConnection = con  
rst.Source = "SELECT AdditionalContactInfo FROM Person.Contact " _  
   & "WHERE AdditionalContactInfo IS NOT NULL"  
rst.Open  
  
' Display the data in the recordset.  
While (Not rst.EOF)  
   sXMLResult = rst.Fields("AdditionalContactInfo").Value  
   Debug.Print (sXMLResult)  
   rst.MoveNext  
End While  
  
con.Close  
Set con = Nothing  

Note

Recordset filtering is not supported with XML columns. If used, an error will be returned.

Retrieving UDT Column Data

In this example, a Command object is used to execute a SQL query that returns a UDT, the UDT data is updated, and then the new data is inserted back into the database. This example assumes that the Point UDT has already been registered in the database.

Dim con As New ADODB.Connection  
Dim cmd As New ADODB.Command  
Dim rst As New ADODB.Recordset  
Dim strOldUDT As String  
Dim strNewUDT As String  
Dim aryTempUDT() As String  
Dim strTempID As String  
Dim i As Integer  
  
con.ConnectionString = "Provider=SQLNCLI11;" _  
         & "Server=(local);" _  
         & "Database=AdventureWorks;" _   
         & "Integrated Security=SSPI;" _  
         & "DataTypeCompatibility=80;"  
  
con.Open  
  
' Get the UDT value.  
Set cmd.ActiveConnection = con  
cmd.CommandText = "SELECT ID, Pnt FROM dbo.Points.ToString()"  
Set rst = cmd.Execute  
strTempID = rst.Fields(0).Value  
strOldUDT = rst.Fields(1).Value  
  
' Do something with the UDT by adding i to each point.  
arytempUDT = Split(strOldUDT, ",")  
i = 3  
strNewUDT = LTrim(Str(Int(aryTempUDT(0)) + i)) + "," + _  
   LTrim(Str(Int(aryTempUDT(1)) + i))  
  
' Insert the new value back into the database.  
cmd.CommandText = "UPDATE dbo.Points SET Pnt = '" + strNewUDT + _  
   "' WHERE ID = '" + strTempID + "'"  
cmd.Execute  
  
con.Close  
Set con = Nothing  

Enabling and Using MARS

In this example, the connection string is constructed to enable MARS through the [!INCLUDEssNoVersion] Native Client OLE DB provider, and then two recordset objects are created to execute using the same connection.

Dim con As New ADODB.Connection  
  
con.ConnectionString = "Provider=SQLNCLI11;" _  
         & "Server=(local);" _  
         & "Database=AdventureWorks;" _   
         & "Integrated Security=SSPI;" _  
         & "DataTypeCompatibility=80;" _  
         & "MARS Connection=True;"  
con.Open  
  
Dim recordset1 As New ADODB.Recordset  
Dim recordset2 As New ADODB.Recordset  
  
Dim recordsaffected As Integer  
Set recordset1 =  con.Execute("SELECT * FROM Table1", recordsaffected, adCmdText)  
Set recordset2 =  con.Execute("SELECT * FROM Table2", recordsaffected, adCmdText)  
  
con.Close  
Set con = Nothing  

In prior versions of the OLE DB provider, this code would cause an implicit connection to be created on the second execution because only one active set of results could be opened per a single connection. Because the implicit connection was not pooled in the OLE DB connection pool this would cause additional overhead. With the MARS feature exposed by the [!INCLUDEssNoVersion] Native Client OLE DB provider, you get multiple active results on the one connection.

See Also

Building Applications with SQL Server Native Client