Virtually all AVR for .NET applications use a DataGate database name to connect to the application’s database server. This article shows how to soft-code a database name in either a Windows or ASP.NET application. In either case, the .NET assembly, System.Configuration, is used for fetching the soft-coded database name. We’ll look at soft-coding a DataGate database name in an ASP.NET application. However, except for the name of the configuration file, the technique is the same for Windows and ASP.NET.
With this technique, we’ll store database names in the project’s config file and then at runtime read the soft-coded DB name with the ConfigurationManager object in the System.Configuration namespace. There are many ways to use this technique, in this method we’ll add a second level of indirection so that you can define many database names in the configuration file. We’ll also add a single key to indicate, of the database names provided, which one is the active database name.
In ASP.NET, open your project’s Web.Config file and look for the file’s node–it is a direct child of the node. In our example, we’re going to add three keys so that it looks like this:
1 2 3 4 5 |
<appSettings> <add key="ActiveDBName" value="LocalDB"/> <add key="LocalDB" value="*Public/DG NET Local"/> <add key="Cypress" value="*Public/Cypress"/> </appSettings> |
In the case of Windows, the configuration isn’t named Web.Config, it is named App.Config. In either case, ASP.NET or Windows, if the appropriate config file isn’t present in your project, add it with “Add New Item.” Make sure to name the ASP.NET config file Web.Config and the Windows config file App.Config.
In this example, two database names are defined, LocalDB and Cypress. These specify the two runtime database names that are available. You can add as many you need. The ActiveDBName key specifies which of the database names provided should be in effect at runtime. With this technique, all you need to do to change the runtime database name is to change the value of the ActiveDBName key. You must be sure that the value you provide for ActiveDBName is among those listed in the runtime database names.
Once your application is deployed you can manage changes to the config file with something as simple as NotePad. Just open the config file and make the changes needed. You can add additional database names or modify existing ones as necessary. This change requires no recompile or redeployment. For ASP.NET apps the changes won’t take effect until the app has been recycled. Note that because of the volatile nature of this config file, be prudent allowing change authority to this config file. In very tight security situations, you’ll probably want to modify this technique so that database names are stored in a secure database (and read with a bootstrap database name). However, especially for ASP.NET applications deployed on an appropriately configured Web server, for most businesses this technique for soft-coding database names is valid as it is presented here.
To see the soft-coded values in play, let’s use them against the DB and DclDiskFile declaration below. Notice the ImpOpen(*No). This is key to a soft-coded database name—you don’t want to open files until the DB has been connected. In Windows apps if the files are implicitly opened, the DB object is also implicitly connected. You don’t want that! For this technique, you need to first connect the DB and then open file(s).
1 2 3 4 5 6 7 8 9 |
DclDB DGServer DBName("*PUBLIC/DG NET Local") DclDiskFile CustomerByName + Type(*Input) + Org(*Indexed) + Prefix(Customer_) + File("Examples/CMastNewL2") + DB(DGServer) + ImpOpen(*No) |
The code below is the code you’d use to connect to the DB specified by the ActiveDBName key. First we fetch the value it specifies as the active database name. AppSettings keys are a collection owned by the ConfigurationManager class—at a glance, AppSettings looks like a method call but it is a collection.
After we fetch the active DB name, then that database name is assigned to the DclDB object before it is connected. This code assumes you have a “Using System.Configuration” at the top of the class.
1 2 3 4 5 6 7 8 9 10 11 |
DclFld ActiveDBName Type(*String) Try ActiveDBName = ConfigurationManager.AppSettings["ActiveDBName"] Catch ActiveDBName = "*Public/DG Net Local" EndTry DGServer.DBName = ActiveDBName Connect DGServer Open CustomerByName |
It is important to put the attempt to read from the config file in a Try/Catch. You might assume that if the key isn’t present a null value would be returned. That isn’t the case. If the key isn’t present, an exception is thrown. If the ActiveDBName key isn’t found, the database name in the Catch part of the clause above acts as the default database name. In production code, you’ll probably also want more defensive error handling about the DB connection and file opens as well. Be sure that every class in your project that connects to a database uses the code above. In a large application it would be prudent to move the code to connect the DB to its own class so you don’t have to repeat this code over and over. (That’s a topic for another article.)
With just a little care, you can easily soft-code database names in your ASNA Visual RPG projects. Do so makes managing database connectivity in real-time much simpler.