Alejandro Garcia
Alejandro Garcia

Reputation: 115

How can I get data from another database in dotnetnuke?

I'm just beginning to develop modules for dotnetnuke (just 2 weeks ago),

I've installed DotNetNuke 6.2 perfectly on XP, I'm working with VS2008, and SQL SERVER 2008 express edition.

I have a database called "dnn" that I use for the tables for framework DNN.

But now I want to get data from another database called Sales (for example) which is inside the same instance of the SQL Server 2008. I really don't have any idea how to connect with this database from my custom user control .aspx. I tried to put the normal code for connect with the database. I did generate the connection string, I did generate the connection, and do the queries from a c# class but this don't work.

Yesterday I found a data access document from dotnetnuke -- but I don't understand how to implement a new connection in my custom module). So my question is, how I can connect with this Sales database?

Upvotes: 1

Views: 3593

Answers (2)

Anthony
Anthony

Reputation: 1709

If you don't plan on selling your module which seems unlikely since you planning to connect to a DB out side of dnn.

I would just use .net

Add System.Configuration to your reference if you don't have it already.

then:

System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionstring"]

in you web config add the new connection string to your connection string section

  <connectionStrings>
    <add name="MyConnectionstring" connectionString="blah" />
 </connectionStrings>

One thing to remember is that DNN is just a frame work around asp.net so you have all the .net data access tools at your disposal.

Using the example above I could write something simple like this:

string connectionstr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionstring"]

using (SqlConnection conn = new SqlConnection(connectionstr ))
{
  // I am using the SqlHelper class here its part of DNN
  sqlstr = "Select * From SomeTable"
  using(SqlReader reader = SqlHelper.ExecuteReader(conn, sqlstr))
  {
    while(reader.read())
    {
     /// read into object or what ever
    }
   }
}

Upvotes: 3

Mitchel Sellers
Mitchel Sellers

Reputation: 63136

I noticed the comments from the other reply and thought I'd add a bit here.

Once you are going to connect to another database, you are outside the relm of the standard DotNetNuke configuration processes. I think you could create a custom data provider/SqlDataProvider setup, but more than likely it isn't that simple.

When working outside the confines of DNN I handle the data access myself directly using SqlHelper and use CBO to help with data hydration but do not work with the DataProvider model for that external database.

Upvotes: 3

Related Questions