Bewerken

Delen via


db_source

Creates a connection to a data source.

Syntax

[ db_source(db_source, name, hresult) ]

Parameters

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.

Remarks

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).

When the consumer attribute provider applies this attribute to a class, the compiler will rename the class to _YourClassNameAccessor, where YourClassName is the name you gave the class, and the compiler will also create a class called YourClassName, which derives from _YourClassNameAccessor. In Class View, you will see both classes.

For an example of this attribute used in an application, see MultiRead.

Example

This sample 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_1.cpp
// compile with: /LD
#include <atlbase.h>
#include <atlplus.h>
#include <atldbcli.h>

[
  db_source(L"my_connection_string", name="ds"),
  db_command(L"select * from Products")
]
class CMyCommand {};

Requirements

Attribute context Value
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.

See also

OLE DB Consumer Attributes