For last couple of weeks I've been developing a custom module for MojoPortal website.
If you’ve followed the tutorials from Joe, you would build two projects to interact with sql data – data access layer and business layer project to allow support multiple databases (MySQL, MS SQL etc.).
I only needed to support MS SQL and as a big fan of LINQ (also being lazy) I’ve decided to use it instead of building data access layer for my project. So I’ll show you how I did that.
To simplify it we are going to build only the UI project. Create a new web application project in your mojoPortal solution. You will need to add all the references and required files. Have a look at the mojoPortal website for more details.
Now add a new data context file “abcLinq.dbml”. Open the abcLinq.dbml file and add some tables from your server explorer and save the dbml file.
The problem is that our abcLinqDataContext connection is now setup in the web.config or if you set abcLinqDataContext property connection - application settings to False then the abcLinqDataContext connection is hardcoded in the constructor (have a look at abcLinqDataContext()).

However if we want to use the connection setup in our user.config file “MSSQLConnectionString” we need to create a new class which inherits from abcLinqDataContext class. Here is the code for this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace abcLinqUI
{
public class abcLinq_Ext : abcLinqDataContext
{
public abcLinq_Ext() :
base(global::System.Configuration.ConfigurationManager.AppSettings["MSSQLConnectionString"])
{
}
}
}
Now we can easily use this in our code. Note that we are now using the abcLinq_Ext() constructor.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace abcLinqUI
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
abcLinq_Ext db = new abcLinq_Ext();
var users = from u in db.mp_Users
where u.Trusted == true
select u;
GridView1.DataSource = users;
GridView1.DataBind();
}
}
}
6b08354c-a9a8-4dd5-8352-f5c3a9d6e96b|1|5.0