1

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?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Alejandro Garcia
  • 115
  • 3
  • 10

2 Answers2

2

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
    }
   }
}
Anthony
  • 1,651
  • 1
  • 17
  • 31
  • Thanks Anthony for your answer!, but now I get another cuestion!, Don't I do the connection at Sales Database using the dnn class?(like DataProvider, SqlDataProvider), Can I define my own SqlDataProvider class to implement this connection. this is what i want do. I've been Reading a Data Access Manual in which, first i need to configure the web.config file adding a new connection string and next i have to specify the provider that will use this other connection string. And now I have to implement a new SqlDataProvider class for this connection. do I can do that? How? Excuse my ignorance. – Alejandro Garcia Jul 11 '12 at 15:49
  • I updated my answer to include some of what @Mitchel was saying, but I didn't have time to dig back into the class hydration stuff. – Anthony Jul 11 '12 at 22:32
  • Ok Anthony, I'm going to take your advice, and I'll connect with this way!, thanks . I'm going to study more about DNN. I have just 2 weeks as a dnn's modules developer for myself, Thanks Again for your answers and if there are more will be welcome. – Alejandro Garcia Jul 11 '12 at 22:49
  • @AlejandroGarcia If you like my answer could you mark it as correct thanks! – Anthony Jul 11 '12 at 23:14
  • Wooow I already could connect to my database! andd Works Jaja Thank you Again! – Alejandro Garcia Jul 13 '12 at 21:29
2

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.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • thank you Mitchel Sellers, I have already noticed it! Woow Mitchel Sellers has written to my question!!!! I began study your book "Professional DotNetNuke Module Programming (Wrox Programmer to Programmer)", Cute! – Alejandro Garcia Jul 11 '12 at 22:57