Search code examples
sql-server-2008visual-studio-2008dotnetnukedotnetnuke-module

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?


Solution

  • 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
        }
       }
    }