Using LINQ to SQL and ConnectionStrings in a Class Library

Posted by: Jason N. Gaylords Blog, on 20 May 2008 | View original | Bookmarked: 0 time(s)

I was looking to separate my LINQ to SQL data layer from the rest of my application. So, I decided to move the files to a class library. This way, when my application builds, all of my LINQ files are found in MyApp.DL or something similar. However, I wanted the class library to use the SQL connection that I have set in my web.config. Because the class library does not have a reference to System.Configuration, I had to add that first. I then created a partial class for my Context object. Inside, I created a new constructor with a boolean parameter called UseWebConfigConnectionString. My web.config would contain a generic connection string name called LinqSqlConnectionString.This allows me to make changes to the LINQ files, save them, and not affect this method. So, here's what I ended up with:

web.config

<connectionStrings>
    <clear/>
    <add name="LinqSqlConnectionString" connectionString="{insert settings here};" providerName="System.Data.SqlClient"/>
</connectionStrings>

MyApp.DL.ExtendedLinqObjects.vb

Imports System.Configuration

Partial Public Class MyDataContext
    Public Sub New(ByVal UseWebConfigConnectionString As Boolean)
        MyBase.New(ConfigurationManager.ConnectionStrings("LinqSqlConnectionString").ConnectionString, mappingSource)
        OnCreated()
    End Sub
End Class

sample.aspx.vb

Dim db As New MyDataContext(True)

This seemed to be the easiest way to pull from my connection strings setting. Although, I'd highly recommend encrypting your connection string before deploying to production.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: SQL | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 589 | Hits: 19

Similar Posts

  • New Entity Framework Feature CTP for VS2010 Beta 2 more
  • Introducing Versatile DataSources more
  • Migrated from Community Server to DasBlog more
  • Entirely unobtrusive and imperative templates with Microsoft Ajax Library Preview 6 more
  • RadGrid for ASP.NET Ajax and DomainDataSource more
  • Building a class browser with Microsoft Ajax 4.0 Preview 5 more
  • Next version of EF Code Only Design laid out by MS more
  • An Extensive Examination of LINQ: Grouping and Joining Data more
  • An Overview of Partial Classes and Partial Methods more
  • Using Stored Procedures for CRUD more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD