+1-888-365-2779
Try Now
More in this section

Forums / Deployment / Deploying with OpenAccess

Deploying with OpenAccess

9 posts, 0 answered
  1. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    04 Jun 2009
    Link to this post
    Quick question and I hope this belongs in the deployment section as in my mind it would only come into play during deployment.

    I using OpenAccess for my DAL in all of my current Sitefinity Pluggable module development. Everything is fine up to the point where I need to deploy my .dll to a new site. Currently I am having to change the app.config to point at the correct DB and then recompile and then deploy. Of course this will not work for very long, so my question is how do I get OpenAccess to read the Sitefinity default DB connection and use it?

  2. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    10 Jun 2009
    Link to this post
    Hello Garry Clark,

    Here is an example:

    using System;   
    using System.Configuration;   
    using System.Data.Common;   
    using System.Data.SqlClient;   
    using System.Reflection;   
    using System.Xml;   
    using Telerik.OpenAccess;   
       
    /// <summary>   
    /// Data access helper class.   
    /// </summary>   
    public static class DataAccess   
    {   
        public static Database GetDatabase()   
        {   
            if (database == null)   
            {   
                // Your connection string name.   
                var connenctionName = "Sitefinity";   
     
                // Since OpenAccess caches metadata and connection information  
                // together using the connection ID as key we have to provide  
                // unique connection ID for each assembly. 
                var assembly = typeof(DataAccess).Assembly; 
                var connId = assembly.FullName; 
     
                database = Database.Get(connId,    
                    GetConfiguration(connenctionName, connId),    
                    new Assembly[] { assembly  });   
            }   
            return database;   
        }   
       
        public static XmlElement GetConfiguration(string connectionName, string connId)   
        {   
            // You can configure OpenAccess connections in web.config.   
            // In that case you have to have defined a connection with id = connectionName   
            var oaConfig = (XmlElement)ConfigurationManager.GetSection("openaccess");   
            if (oaConfig != null
            { 
                // You may need to replace the value of "id" attribute in  
                // "connection" element with the value of "connId" parameter.  
                return oaConfig;  
            } 
       
            // OR you can parse standard ASP.NET connection strings.   
       
            string backend, databaseName, serverName, username, password;   
            var connString = ConfigurationManager.ConnectionStrings[connectionName];   
            var  dbFactory = DbProviderFactories.GetFactory(connString.ProviderName);   
       
            if (dbFactory is SqlClientFactory)   
            {   
                // The OpenAccess Database configuration.   
                backend = "mssqlConfiguration";   
       
                var connBuilder = new SqlConnectionStringBuilder(connString.ConnectionString);   
       
                serverName = connBuilder.DataSource;   
                databaseName = connBuilder.InitialCatalog;   
                username = connBuilder.UserID;   
                password = connBuilder.Password;   
            }   
            else   
            {   
                // If you want to support other databases then MS SQL you could    
                // decide the correct OpenAccess configuration based on the client factory.   
                throw new NotSupportedException();   
            }   
       
            string connectionNode =   
                string.Format(   
                    @"<openaccess xmlns=""http://www.telerik.com/OpenAccess"">   
                        <connections>   
                          <connection id=""{0}"">   
                            <databasename>{1}</databasename>   
                            <servername>{2}</servername>   
                            <integratedSecurity>{3}</integratedSecurity>   
                            <user>{4}</user>   
                            <password>{5}</password>   
                            <backendconfigurationname>{6}</backendconfigurationname>   
                            <reference assemblyname=""{7}""/>   
                          </connection>   
                        </connections> 
                      </openaccess>",   
                    connId,   
                    databaseName,   
                    serverName,   
                    string.IsNullOrEmpty(username),   
                    username, password,   
                    backend,   
                    typeof(DataAccess).Assembly.FullName);   
       
            var doc = new XmlDocument();   
            doc.LoadXml(connectionNode);   
       
            return  doc.DocumentElement;   
        }   
       
        private static Database database;   
    }   
     


    Sincerely yours,
    Bob
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    23 Jun 2009
    Link to this post
    Bob,
    This is a very cool solution. Thank you for posting this. I have yet to test it in a live deployment, but regardless it put me on the right track.

    One correction in case anyone else needs to implement this is you forgot to close the <openaccess> tag in your XML markup. Not a huge deal, but just wanted peeps to be aware of it.

    Thanks as always!

    Corrected XML String (Code Block wouldn't work so apologies for the straight paste)

    string

     

    connectionNode =

     

     

    string.Format(

     

     

    @"<openaccess xmlns=""http://www.telerik.com/OpenAccess"">

     

    <connections>

    <connection id=""{0}"">

    <databasename>{1}</databasename>

    <servername>{2}</servername>

    <integratedSecurity>{3}</integratedSecurity>

    <user>{4}</user>

    <password>{5}</password>

    <backendconfigurationname>{6}</backendconfigurationname>

    <reference assemblyname=""{7}""/>

    </connection>

    </connections>

    </openaccess>"

     

    ,

     

    connectionName,

    databaseName,

    serverName,

     

    string.IsNullOrEmpty(username),

     

    username, password,

    backend,

     

    typeof(DataAccess).Assembly.FullName);

     

  4. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    09 Jul 2009
    Link to this post
    Hi Garry,

    Thank you for the correction, I have updated the code in the previous post to reflect it.

    I also did slight change, now the assembly full name is used for connection ID instead of the connection string name. This change should allow the use of multiple assemblies with the same database.

    Regards,
    Bob
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    09 Jul 2009
    Link to this post
    Bob,
    If I could buy you a beer and a steak I would. Your code worked wonderfully and also resolved my support ticket in a more dynamic way. Two birds with stone is always awesome! I think this should definetly be turned into a KB article unless 4.0 will change the way we use OpenAccess with Sitefinity. 

    The support at Telerik, as always, out did my expectations!

    Thanks!
  6. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    10 Jul 2009
    Link to this post
    Garry,

    I'm glad this solves the problem.

    In Sitefinity 4.0 you don't need to wary about any of that. All you will have to do is to configure properly your persistent classes and Sitefinity will take care of the rest, including database schema update on the fly.

    Best wishes,
    Bob
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    10 Jul 2009
    Link to this post
    Great news thanks again Bob!

    Last thing and I promise I'll offically leave this thread alone. Any updates on the possible release of 4.0 or beta testing it?
  8. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    13 Jul 2009
    Link to this post
    Garry,

    Ugh, this is probably the most difficult question you could ever ask me :) 

    We are already behind our internal schedule, but I'm pretty confident the first public preview and beta release will be held in the beginning of October.

    Best wishes,
    Bob
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    13 Jul 2009
    Link to this post
    Haha I can understand that. No worries just thought I'd poke the proverbial cauldron of mystery and see what I could get out of it!

    Good luck as the Sitefinty community is just dying to see 4.0 in action!
Register for webinar
9 posts, 0 answered