DataContext Constructor (String)
Initializes a new instance of the DataContext class by referencing a file source.
Assembly: System.Data.Linq (in System.Data.Linq.dll)
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.
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.
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")
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
- 11/25/2011
- justsml
I agree
Nevertheless I've been able to instantiate this class successfully by passing the connection string from my web.config file, such as:
Although it lacks an example of constructor with the other 2 possiilities, which I could not figure out by myself yet.string connStr = System.Configuration.ConfigurationManager.AppSettings["MyConnStr"].ToString();
DataContext dc = new DataContext(connStr);
- 11/10/2011
- WeMoecke
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.
- 4/20/2011
- JCandTheGirls
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.
- 12/14/2010
- Roger Crawfis