Passing LINQ a dynamic connection string

  • Thread starter Thread starter Dean Slindee
  • Start date Start date
D

Dean Slindee

VS2008, .NetFramework 3.5 SP1:

I have built a LINQ data access layer project. When the LINQ data context
was built over an existing SQL2005 database, the connection string for that
database was correctly, automatically, placed into the Settings for the LINQ
project. It works, but it's a "static" connection string.

I would like to be able to use this one LINQ data access layer against both
test and production databases without having to manually change and
recompile the project's connection string each time. Since this is a data
access layer project, the functions within it are called by the User
application project. The User application project has the test or
production connection string within it's settings.

Is there a way to pass the User connection string to the LINQ data access
layer? I would even consider passing the connection string on each function
call to the LINQ project, but my first attempt was unsuccessful, because the
LINQ connection string is ReadOnly.

Public Shared Function GetModuleRowsActiveBoot(ByVal strConnectionString
As String) As DataTable
Dim dt As New DataTable
Try
Dim dc As New dcSecurityDataContext
Global.SecurityLinq.My.MySettings.Default.SecurityConnectionString
= strConnectionString

The last statement above is in error because
Global..SecurityConnectionString is ReadOnly.

Anyone tried doing this?
 
VS2008, .NetFramework 3.5 SP1:

I have built a LINQ data access layer project. When the LINQ data context
was built over an existing SQL2005 database, the connection string for that
database was correctly, automatically, placed into the Settings for the LINQ
project. It works, but it's a "static" connection string.

I would like to be able to use this one LINQ data access layer against both
test and production databases without having to manually change and
recompile the project's connection string each time. Since this is a data
access layer project, the functions within it are called by the User
application project. The User application project has the test or
production connection string within it's settings.

Is there a way to pass the User connection string to the LINQ data access
layer? I would even consider passing the connection string on each function
call to the LINQ project, but my first attempt was unsuccessful, because the
LINQ connection string is ReadOnly.

Public Shared Function GetModuleRowsActiveBoot(ByVal strConnectionString
As String) As DataTable
Dim dt As New DataTable
Try
Dim dc As New dcSecurityDataContext
Global.SecurityLinq.My.MySettings.Default.SecurityConnectionString
= strConnectionString

The last statement above is in error because
Global..SecurityConnectionString is ReadOnly.

Anyone tried doing this?

That code appears to be incorrect. Doesn't the DataContext derived
class take the connection string in its constructor? In any event,
you can store the connection string in a configuration file of xml
file and read it from there. Since it is just a string, you could
also create one dynamically at runtime or let the user choose one.
There are any number of ways to handle this.

Chris
 
Back
Top