This documentation is archived and is not being maintained.


Creates a connection to a data source.

[ db_source( 
) ]


The connection string used to connect to the data source. For the format of the connection string, see Connection Strings and Data Links in the Microsoft Data Access Components (MDAC) SDK.
name (optional)
When you use db_source on a class, name is an instance of a data source object that has the db_source attribute applied to it (see example 1). When you use db_source inline in a method implementation, name is a variable (local to the method) that can be used to access the data source (see example 2). You pass this name to the source_name parameter of db_command to associate the data source with a command.
hresult (optional)
Identifies the variable that will receive the HRESULT of this database command. If the variable does not exist, it will be automatically injected by the attribute.

Attribute Context

Applies to class, struct, member, method, local
Repeatable No
Required attributes None
Invalid attributes None

For more information about the attribute contexts, see Attribute Contexts.


db_source creates a CDataSource and a CSession object, which together represent a connection with an OLE DB consumer data source.

When you use db_source on a class, the CSession object becomes a member of the class.

When you use db_source in a method, the injected code will be executed within method scope, and the CSession object is created as a local variable.

db_source adds data source properties to a class or within a method. It is used in conjunction with db_command (which takes the db_source name parameter as its source_name parameter).


Example 1 calls db_source on a class to create a connection to the data source ds using the Northwind database. ds is a handle for the data source, which can be used internally to the CMyCommand class.

   db_source(L"my_connection_string", name=ds)
   db_command(L"select * from Products")
class CMyCommand

Example 2 calls db_source within a method to create a connection to the data source CMySource using the Northwind database. The data source object is ds and the rowset object is rs.

CMySource ds;
void MakeConnections()
[db_source(L"my_connection_string", name=ds)]
[db_command(L"Select * from Products", name=rs, source_name=ds)]

For an example of this attribute used in an application, see the samples AtlAgent and MultiRead.

See Also

OLE DB Consumer Attributes | Attributes Samples