This topic has not yet been rated - Rate this topic

DataContext Constructor (String)

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

Namespace:  System.Data.Linq
Assembly:  System.Data.Linq (in System.Data.Linq.dll)
public DataContext(
	string fileOrServerOrConnection
)

Parameters

fileOrServerOrConnection
Type: System.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.

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
DataContext Connection String Solution
Use connection string  as you use for System.Data.SqlClient.SqlConnection.
e.g.

    Dim db As New DataContext("server=.\sqlexpress;database=sales;uid=sa;pwd=12345678")
Just FYI - Connection String examples
$0$0 $0 Connection String examples in case anyone needs them: $0http://www.connectionstrings.com/sql-server-2008$0 $0$0 $0 $0$0 $0 $0$0 $0
I agree
Nevertheless I've been able to instantiate this class successfully by passing the connection string from my web.config file, such as:

string connStr = System.Configuration.ConfigurationManager.AppSettings["MyConnStr"].ToString();

DataContext dc = new DataContext(connStr);


Although it lacks an example of constructor with the other 2 possiilities, which I could not figure out by myself yet.
SQL Database connection example
I agree with Roger Crawfis. Just a basic example showing how to connect to a SQL DB would save a lot of time.
String passed to constructor needs more explanation.
It would be nice to have a link that describes the format of the string for this constructor.