Step 3: Setting up Data Accesss for Your WPF Application

This step assumes you have already created a database to be accessed from your application.

  1. In the Solution Explorer under the business object project, right-click the ABusinessObject.cs or ABusinessObject.vb file and select View Code from the shortcut menu. This file contains the ABusinessObject class definition, which is the base class for all business objects in your application. In the ABusinessObject class, change the value assigned to the DatabaseKey property to a name descriptive of your application's database.

    For example, in C#:

    public class ABusinessObject<EntityType> : mmBusinessObjectGeneric<EntityType>
    	where EntityType : mmBusinessEntity, new()
    {
    	/// <summary>
    	/// Constructor
    	/// </summary>
    	public ABusinessObject()
    	{
    		// Enter the default database key as specified in the app.config file
    		this.DatabaseKey = "MyDatabase";
    
    		// Specify the default command type for data retrieval
    		this.DefaultCommandType = CommandType.StoredProcedure;
    	}
    }

    In VB .NET:

    Public Class ABusinessObject(Of EntityType As {mmBusinessEntity, New})
        Inherits mmBusinessObjectGeneric(Of EntityType)
    
        ''' <summary>
        ''' Constructor
        ''' </summary>
    	Public Sub New()
    
    		' Enter the default database key as specified in the app.config file
    		Me.DatabaseKey = "MyDatabase"
    
    		' Specify the default command type for data retrieval
    		Me.DefaultCommandType = CommandType.StoredProcedure
    
    	End Sub
    
    End Class

  2. IMPORTANT: In the Solution Explorer, right-click your business object project and select Properties from the shortcut menu. If there are spaces in the Assembly Name you need to remove the spaces or the business objects cannot be referenced properly from your WPF application (a WPF "issue", if you will). Using your project's default or root namespaces as the assembly name often makes the most sense.

  3. By default, the ABusinessObject class assumes you are using stored procedures to retrieve data from the back end. If you prefer to use dynamic SQL by default, go to the constructor of ABusinessObject and change the setting of the CommandType property to CommandType.Text

  4. If you are using dynamic SQL to update back end tables (rather than using stored procedures) and if the majority of business objects in your application work with back end tables that have auto-increment columns (such as SQL Server identity columns), set the RetrieveAutoIncrementPK property of ABusinessObject to True. If this property is true when saving a new record, the new auto-increment value is automatically retrieved from the database table and stored in the primary key column of the newly saved record.

    Here's the code in C#:

    	public class ABusinessObject<EntityType> : mmBusinessObjectGeneric<EntityType>
    	where EntityType : mmBusinessEntity, new()
    {
    	/// <summary>
    	/// Constructor
    	/// </summary>
    	public ABusinessObject()
    	{
    		// Enter the default database key as specified in the app.config file
    		this.DatabaseKey = "MyDatabase";
    
    		// Specify the default command type for data retrieval
    		this.DefaultCommandType = CommandType.StoredProcedure;
    
    		this.RetrieveAutoIncrementPK = true;
    	}
    }

    And in VB .NET:

    Public Class ABusinessObject(Of EntityType As {mmBusinessEntity, New})
        Inherits mmBusinessObjectGeneric(Of EntityType)
    
        ''' <summary>
        ''' Constructor
        ''' </summary>
    	Public Sub New()
    
    		' Enter the default database key as specified in the app.config file
    		Me.DatabaseKey = "MyDatabase"
    
    		' Specify the default command type for data retrieval
    		Me.DefaultCommandType = CommandType.StoredProcedure
    
    		Me.RetrieveAutoIncrementPK = True
    
    	End Sub
    
    End Class

    If you set this value at the ABusinessObject level but you have a business object subclass that does NOT work with a back end table that has an auto-increment column, you should set the business object's RetrieveAutoIncrementPK property to False.

  5. In the Solution Explorer, double-click your project's app.config file to open it in the code-editing window. The configuration file contains settings that tell the application about each database used by the application. You should see the following element:

    <databases>
      <add key="Northwind\Connection" value="server=(local);uid=sa;pwd=;database=NorthWind;" />
      <add key="Northwind\DataAccessClass" value="DataAccessSql" />
    </databases>

  6. Add a new element to this file for each database in your application (if you'd like, you can remove the default NorthWind database element). For example, if your application accesses two SQL Server databases named MainData and Common, your configuration file might look something like this:

    <databases>
      <add key="MainData\Connection" value="server=(local);uid=sa;pwd=;database=MainData;" />
      <add key="MainData\DataAccessClass" value="DataAccessSql" />
      <add key="Common\Connection" value="server=(local);uid=sa;pwd=;database=Common;" />
      <add key="Common\DataAccessClass" value="DataAccessSql" />
    </databases>


    Note: The database key value (MainData, for example) is not case sensitive. In your ABusinessObject class, you could set the DatabaseKey property to "MAINDATA", but specify the key value as "maindata" in the configuration file and it will still work. In addtion, the key name does not need to be exactly the same as the actual database name. For example, your database may be named "Northwind", but you could specify a database key of "NWind".

Additional Information on Setting Up Data Access

The above example shows how to specify a SQL Server connection when using SQL Server Authentication. For more information on connection strings see the topic Specifying Connection Strings in the Config File.

If you need to access multiple databases from your application, check out Data Access with MM .NET for details.

Alternately, you can specify the data access class in your application configuration file.


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 02/12/26
Comment or report problem with topic