Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Dynamically Manage a Connection to a Data Store
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Connections and Data Store Management > How to: Dynamically Manage a Connection to a Data Store

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

This topic shows how to define the use of a database that is not attached to any database-server and attaches itself dynamically. 

  • Open the application's configuration file (App.config)
  • In  the "connection" section add the "conncetionParams" tag and write "AttachDbFilename=myfolder\PathToMyDatabase\myDatabase.mdf" as an inner text.

 The resulting section of the App.config should look like this:

App.config Copy Code
<connections>
     
<connection id="DatabaseConnection1">
       
<databasename>Northwind</databasename>
       
<servername>.\SQLEXPRESS</servername>
       
<integratedSecurity>True</integratedSecurity>
       
<backendconfigurationname>mssqlConfiguration</backendconfigurationname>      <connectionParams>AttachDbFileName=C:\Work\NORTHWND.mdf;</connectionParams>
      
</connection>
</
connections>

 

First Scenario:

Web Site project needs to be runed from several deferent locations. The database it uses is stored in the App_Data folder. Here are the steps that need to be taken to have the project running on any machine out of the box:

  • Create a new WebSite and prepare it to use OpenAccess
  • Add an "openaccess" configuration section inside the <configsections> tab of the web.config
    Web.config Copy Code
    <configSections>
    ...
    <section name="openaccess" type="Telerik.OpenAccess.Config.ConfigSectionHandler, Telerik.OpenAccess.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7ce17eeaf1d59342"/>

    </configSections>
  • Configure the given section in the following way:
Web.config Copy Code
<openaccess xmlns="http://www.telerik.com/orm">
   <references>
     
<reference assemblyname="ClassLibrary1" configrequired="True" />
   
</references>
   
<connections>
     
<connection id="DatabaseConnection1">
       
<databasename>NORTHWIND</databasename>
       
<servername>(LOCAL)\SQLEXPRESS</servername>
       
<integratedSecurity>True</integratedSecurity>
       
<backendconfigurationname>mssqlConfiguration</backendconfigurationname>
       
<connectionParams>AttachDbFileName=|DataDirectory|\NORTHWND.mdf;</connectionParams>
     
</connection>
   
</connections>
 
</openaccess>.

 

  • The assembly that holds the persistent-model (in this case "ClassLibrary1") which is going to be used in the web project should be defined in the "references" sub-section of the "openaccess" section.

This way when executing the code below an IObjectScope instance exposing the datababase .mdf file stored in the App_Data directory will be returned.

C# Copy Code
protected void Page_Load(object sender, EventArgs e)
{
IObjectScope scope = Database.Get(
"DatabaseConnection1").GetObjectScope();
...
}

 

VB .NET Copy Code
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim scope As IObjectScope = Database.Get("DatabaseConnection1").GetObjectScope()
...

End Sub

 

Second Scenario:

A project runs on different machines and the database settings are unknown at design-time. The connection section of the configuration file has to be built completely at runtime.
In order to achieve that easily, an autogenerated ObjectScopeProvider helper class can be used. There is a method in the class which takes care of obtaining dynamic connections. This method is named AdjustForDynamicLoad and usually takes no arguments. 

C# Copy Code
static public void AdjustForDynamicLoad()
{
   
if (theObjectScopeProvider1 == null)
       theObjectScopeProvider1 =
new ObjectScopeProvider1();
   
if (theObjectScopeProvider1.myDatabase == null)
       {
          
string assumedInitialConfiguration =
                          
"<openaccess>" +
                              
"<references>" +
                                  
"<reference assemblyname='PLACEHOLDER' configrequired='True'/>" +
                              
"</references>" +
                          
"</openaccess>";
          System.Reflection.Assembly dll = theObjectScopeProvider1.GetType().Assembly;
          assumedInitialConfiguration = assumedInitialConfiguration.Replace(
                                                   
"PLACEHOLDER", dll.GetName().Name);
          System.Xml.XmlDocument xmlDoc =
new System.Xml.XmlDocument();
          xmlDoc.LoadXml(assumedInitialConfiguration);
          Database db = Telerik.OpenAccess.Database.Get(
"DatabaseConnection1",
                                           xmlDoc.DocumentElement,
                                           
new System.Reflection.Assembly[] { dll });
          theObjectScopeProvider1.myDatabase = db;
       }
}
VB .NET Copy Code
Public Shared Sub AdjustForDynamicLoad()
        If theObjectScopeProvider1 Is Nothing Then
            theObjectScopeProvider1 = New ObjectScopeProvider1()
        End If
        If theObjectScopeProvider1._myDatabase Is Nothing Then
            Dim assumedInitialConfiguration As String
            assumedInitialConfiguration = "<openaccess>" _
                           & "<references>" _
                           & "<reference assemblyname='PLACEHOLDER' configrequired='True'/>" _
                           & "</references>" _
                           & "</openaccess>"
            Dim repl As String = "PLACEHOLDER"
            Dim dll As System.Reflection.Assembly = theObjectScopeProvider1.GetType().Assembly
            assumedInitialConfiguration = assumedInitialConfiguration.Replace(repl, dll.GetName().Name)
            Dim xmlDoc As New System.Xml.XmlDocument
            xmlDoc.LoadXml(assumedInitialConfiguration)
            Dim allDlls As System.Reflection.Assembly() = New System.Reflection.Assembly() { dll }
            theObjectScopeProvider1._myDatabase = Telerik.OpenAccess.Database.Get("DatabaseConnection1", _
           xmlDoc.DocumentElement, allDlls)
        End If
    End Sub

Nevertheless, this method can be modified to supply user’s needs.  The following code snippet illustrates building of the whole connection section of the configuration and attaching an external database file to the server.

C# Copy Code
static public void AdjustForDynamicLoad(string myPath)
{
if (theObjectScopeProvider1 == null)
 theObjectScopeProvider1 =
new ObjectScopeProvider1();
if (theObjectScopeProvider1.myDatabase == null)
{
 
string assumedInitialConfiguration =
      
"<openaccess>" +
       
"<references>" +
        
"<reference assemblyname='PLACEHOLDER' configrequired='True'/>" +
       
"</references>" +
       
"<connections>" +
      
@"<connection id=""DatabaseConnection1"">" +
       
"<databasename>-</databasename>" +
         
@"<servername>.\SQLEXPRESS</servername>" +
       
"<integratedSecurity>True</integratedSecurity>" +
         
@"<connectionParams>AttachDbFilename=" + myPath + "</connectionParams>" +
       
"<backendconfigurationname>mssqlConfiguration</backendconfigurationname>" +
      
"</connection>" +
     
"</connections>" +
      
"</openaccess>";
 System.Reflection.Assembly dll = theObjectScopeProvider1.GetType().Assembly;
 assumedInitialConfiguration = assumedInitialConfiguration.Replace(
          
"PLACEHOLDER", dll.GetName().Name);
 System.Xml.XmlDocument xmlDoc =
new System.Xml.XmlDocument();
 xmlDoc.LoadXml(assumedInitialConfiguration);
 Database db = Telerik.OpenAccess.Database.Get(
"DatabaseConnection1",
        xmlDoc.DocumentElement,
new System.Reflection.Assembly[] { dll });
 theObjectScopeProvider1.myDatabase = db;
}
}
VB .NET Copy Code
Public Shared Sub AdjustForDynamicLoad(ByVal myPath As String)
   If theObjectScopeProvider1 Is Nothing Then
    theObjectScopeProvider1 = New ObjectScopeProvider1()
   End If
   If theObjectScopeProvider1._myDatabase Is Nothing Then
    Dim assumedInitialConfiguration As String = "<openaccess>" _
              & "<references>" _
              & "<reference assemblyname='PLACEHOLDER' configrequired='True'/>" _
              & "</references>" _
              & "<connections>" _
              & "<connection id=""DatabaseConnection1"">" _
              & "<databasename>-</databasename>" _
              & "<servername>.\SQLEXPRESS</servername>" _
              & "<integratedSecurity>True</integratedSecurity>" _
              & "<connectionParams>AttachDbFilename=" & myPath & "</connectionParams>" _
              & "<backendconfigurationname>mssqlConfiguration</backendconfigurationname>" _
              & "</connection>" _
              & "</connections>" _
              & "</openaccess>"
    Dim dll As System.Reflection.Assembly = theObjectScopeProvider1.GetType().Assembly
    assumedInitialConfiguration = assumedInitialConfiguration.Replace("PLACEHOLDER", dll.GetName().Name)
    Dim xmlDoc As New System.Xml.XmlDocument()
    xmlDoc.LoadXml(assumedInitialConfiguration)
    Dim db As Database = Telerik.OpenAccess.Database.Get("DatabaseConnection1", xmlDoc.DocumentElement, New System.Reflection.Assembly() { dll })
    theObjectScopeProvider1.myDatabase = db
   End If
End Sub

The Database.Get method accepts a XmlElement that fully overrides the corresponding section in the configuration file. When using this code just make sure it is executed before creating an ObjectScope instance.

C# Copy Code
String dbPath = ”\\Northwind.mdf”
ObjectScopeProvider1.AdjustForDynamicLoad(dbPath);
IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
VB .NET Copy Code
Dim dbPath As String = "C:\\Northwind.mdf"
ObjectScopeProvider1.AdjustForDynamicLoad(dbPath)
Dim scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope()

Using a helper class such as the ObjectScopeProvider is not mandatory but it is the better approach. The other way is to deal with XML configurations directly in the business logic:

C# Copy Code
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(configuration);
Database db = Database.Get(
"DatabaseConnection1", xmlDoc.DocumentElement);
IObjectScope scope = db.GetObjectScope();
VB .NET Copy Code
Dim xmlDoc As New System.Xml.XmlDocument()
xmlDoc.LoadXml(configuration)
Dim db As Database = Database.Get("DatabaseConnection1", xmlDoc.DocumentElement)
Dim scope As IObjectScope = db.GetObjectScope()