DataContext Constructors

Definition

Initializes a new instance of the DataContext class. The connection string you use can be an ADO.NET connection string. You can also specify a filename to a SQL Server Express or SQL Server Compact file.

Overloads

DataContext(IDbConnection)

Initializes a new instance of the DataContext class by referencing the connection used by the .NET Framework.

DataContext(String)

Initializes a new instance of the DataContext class by referencing a file source.

DataContext(IDbConnection, MappingSource)

Initializes a new instance of the DataContext class by referencing a connection and a mapping source.

DataContext(String, MappingSource)

Initializes a new instance of the DataContext class by referencing a file source and a mapping source.

DataContext(IDbConnection)

Initializes a new instance of the DataContext class by referencing the connection used by the .NET Framework.

C#
public DataContext(System.Data.IDbConnection connection);

Parameters

connection
IDbConnection

The connection used by the .NET Framework.

Remarks

A DataContext opens and closes a database connection as needed if you provide a closed connection or a connection string. In general, you should never have to call Dispose on a DataContext. If you provide an open connection, the DataContext will not close it. Therefore, do not instantiate a DataContext with an open connection unless you have a good reason to do this. In a System.Transactions transaction, a DataContext will not open or close a connection to avoid promotion.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

DataContext(String)

Initializes a new instance of the DataContext class by referencing a file source.

C#
public DataContext(string fileOrServerOrConnection);

Parameters

fileOrServerOrConnection
String

This argument can be any one of the following: The name of a file where a SQL Server Express database resides.

The name of a server where a database is present. In this case the provider uses the default database for a user.

A complete connection string. LINQ to SQL just passes the string to the provider without modification.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

DataContext(IDbConnection, MappingSource)

Initializes a new instance of the DataContext class by referencing a connection and a mapping source.

C#
public DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mapping);

Parameters

connection
IDbConnection

The connection used by the .NET Framework.

mapping
MappingSource

A source for mapping.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

DataContext(String, MappingSource)

Initializes a new instance of the DataContext class by referencing a file source and a mapping source.

C#
public DataContext(string fileOrServerOrConnection, System.Data.Linq.Mapping.MappingSource mapping);

Parameters

fileOrServerOrConnection
String

This argument can be any one of the following:

The name of a file where a SQL Server Express database resides.

The name of a server where a database is present. In this case the provider uses the default database for a user.

A complete connection string. LINQ to SQL just passes the string to the provider without modification.

mapping
MappingSource

A source for mapping.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1