Connecting your DotNetNuke® Module to another database

Also see:

A question that frequently comes up is "How do I get my module to connect to another database?" This question is not to be confused with "How do I run DotNetNuke using an alternate data provider such as Oracle or MySQL?"

I find this question very odd because I think "Just connect to it the way you normally would". I now think I understand where the confusion comes from. After reading about the DotNetNuke DAL and DAL+, some developers begin to feel that there must be a DotNetNuke "official" way of connecting to another database.

Faster development with the DAL+

The DAL+ provides a way to get to the database with the least amount of code. With the DAL+ you can call the database with one line of code such as:

CType(DataProvider.Instance().ExecuteSQL(mySqlString.ToString()), IDataReader

This is a great tool but it is designed to connect only to the database that DotNetNuke is running on. To connect to another database you have to write your own code. This can turn out to be a lot of code.

If you want to save code and connect to another database other than the one DotNetNuke is running on, you can use the SqlHelper object that is in the Microsoft.ApplicationBlocks.Data assembly that is included in the /bin directory of DotNetNuke. We can use the SqlHelper with one line of code:

SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, mySqlString.ToString())

If you have a development and production environment you can store the connection string in the web.config file.

Note: there is a small risk in using this. There is no guarantee that it will always be included in future versions of DotNetNuke, but it has been there for the last three years. Even if we upgrade to the newest version of the Microsoft Application Blocks the new version still contains the SqlHelper class.

To demonstrate this I created a module (in VB and C#) that shows how to connect to the NorthWind database using Dynamic SQL and a Stored Procedure (there is a link to download the code at the end of this article).


using DotNetNuke;
using System.Web.UI.WebControls;
using System;
using System.Text;
using System.Web;
using Microsoft.VisualBasic;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
namespace ADefWebsrver.Modules.AlternateDAL
    public partial class AlternateDAL : DotNetNuke.Entities.Modules.PortalModuleBase

        string ConnectionString = "Server=(local);Database=Northwind;uid=MyUserName;pwd=MyPassword;";
        StringBuilder mySqlString = new StringBuilder();

        private void ShowData(IDataReader MyIDataReader)
            GridView1.DataSource = MyIDataReader;

        protected void btnExecuteSQL_Click(object sender, EventArgs e)
            mySqlString.Append("SELECT * ");
            mySqlString.Append("FROM Customers ");
            ShowData(SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, 
        protected void btnExecuteReader_Click(object sender, EventArgs e)
            SqlParameter myParam = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
            myParam.Value = "ALFKI";
		CommandType.StoredProcedure, "CustOrderHist", myParam));


Imports DotNetNuke
Imports System.Web.UI.WebControls
Imports System
Imports System.Text
Imports System.Web
Imports Microsoft.VisualBasic
Imports System.Data
Imports Microsoft.ApplicationBlocks.Data

Namespace ADefWebsrver.Modules.AlternateDAL
    Public Class AlternateDAL_VB
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase

        Dim ConnectionString As String = _
        Dim mySqlString As StringBuilder = New StringBuilder

        Private Sub ShowData(ByVal MyIDataReader As IDataReader)
            GridView1.DataSource = MyIDataReader
        End Sub

        Protected Sub DynamicSQL(ByVal sender As Object, ByVal e As EventArgs) _
        Handles btnExecuteSQL.Click
            mySqlString.Append("SELECT * ")
            mySqlString.Append("FROM Customers ")
            ShowData(SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, _
        End Sub

        Protected Sub StoredProcedure(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles btnExecuteReader.Click
            Dim myParam As SqlParameter = New SqlParameter("@CustomerID", SqlDbType.NChar, 5)
            myParam.Value = "ALFKI"
            ShowData(SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, _
            "CustOrderHist", myParam))
        End Sub
    End Class
End Namespace

[Download the source code for this article]

(You will have to open up the code and insert your username and password to connect to your copy of the Northwind database)

Developing Your Own DotNetNuke Module

To develop your own DotNetNuke modules, it is recommended that you follow these tutorials in this order:

DotNetNuke® 4 Module Tutorial Series:

[Back to: The ADefWebserver DotNetNuke HELP WebSite]

DotNetNuke® is a registered trademark of Perpetual Motion Interactive Systems Inc.