App.config and Windows Mobile

Posts   
 
    
skagget
User
Posts: 3
Joined: 16-May-2008
# Posted on: 18-May-2008 22:51:17   

Hi,

I'm currently evaluating LLBLGen Pro, our target platform is Windows CE and Windows Mobile. LLBLGen Pro's database connection string is specified in the App.config file, which unfortunately isn't used on Windows CE/Mobile. Is there an easy work-around to specify the connection string?

Regards,

Johan

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 19-May-2008 07:54:17   

The connection string can be passed to the DataAccessAdapter Ctor.

skagget
User
Posts: 3
Joined: 16-May-2008
# Posted on: 19-May-2008 10:18:09   

There isn't any other way? Such as a static property in DataAccessAdapter (sort of like a default connection string) that can be set?

Regards,

Johan

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39873
Joined: 17-Aug-2003
# Posted on: 19-May-2008 12:53:51   

skagget wrote:

There isn't any other way? Such as a static property in DataAccessAdapter (sort of like a default connection string) that can be set?

Regards,

Johan

No, as the connection string can be set per call. Using a static property would mean that the connection string is the same for all threads. For the CF.NET framework this isnt that much relevant, but for normal .NET apps it is.

So just pass it to the dataaccessadapter in the constructor. I don't see how that's 'not easy', btw...

Frans Bouma | Lead developer LLBLGen Pro
skagget
User
Posts: 3
Joined: 16-May-2008
# Posted on: 22-May-2008 16:01:52   

Otis wrote:

skagget wrote:

There isn't any other way? Such as a static property in DataAccessAdapter (sort of like a default connection string) that can be set?

Regards,

Johan

No, as the connection string can be set per call. Using a static property would mean that the connection string is the same for all threads. For the CF.NET framework this isnt that much relevant, but for normal .NET apps it is.

So just pass it to the dataaccessadapter in the constructor. I don't see how that's 'not easy', btw...

What I wanted was a static connection string property that act as a default value. If there is no connection string value in the App.config file or the connection string parameter to the constructor is omitted the static property is used. That way I can specify the connection string once without the need for App.config. It's not difficult but tedious and easy to forget.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 22-May-2008 16:20:45   

You might want to use a factory class to return instances of the DataAccessAdapter (filled with the connectionString), so you can use it through out your application.

Also you might derive from the DataAccessAdapter, and create the static property.

Rauken
User
Posts: 7
Joined: 02-Jun-2005
# Posted on: 23-May-2008 15:05:22   

Hi!

I also noticed that there is not app.config file in windows mobile. Found this solution on the web. I created a class called ConfigurationManager.cs. Create an xmlfile called appname.exe.config. In the config file place your xml as usual.


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="Connstring" value="put your connection string here" />
    </appSettings>
</configuration>

In the code just use:


string conn = ConfigurationManager.AppSettings.Get("Connstring");

Here's the code for the ConfigurationManager class.



    public static class ConfigurationManager
    {
        #region Private Members

        private static NameValueCollection appSettings = new NameValueCollection();
        private static string configFile;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets configuration settings in the appSettings section.
        /// </summary>
        public static NameValueCollection AppSettings
        {
            get
            {
                return appSettings;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Static constructor.
        /// </summary>
        static ConfigurationManager()
        {
            // Determine the location of the config file
            ConfigurationManager.configFile = String.Format("{0}.config", System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

            // Ensure configuration file exists
            if (!File.Exists(ConfigurationManager.configFile))
            {
                throw new FileNotFoundException(String.Format("Configuration file ({0}) could not be found.", ConfigurationManager.configFile));
            }

            // Load config file as an XmlDocument
            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load(ConfigurationManager.configFile);

            // Add keys and values to the AppSettings NameValueCollection
            foreach (XmlNode appSettingNode in myXmlDocument.SelectNodes("/configuration/appSettings/add"))
            {
                ConfigurationManager.AppSettings.Add(appSettingNode.Attributes["key"].Value, appSettingNode.Attributes["value"].Value);
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Saves changes made to the configuration settings.
        /// </summary>
        public static void Save()
        {
            // Load config file as an XmlDocument
            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load(ConfigurationManager.configFile);

            // Get the appSettings node
            XmlNode appSettingsNode = myXmlDocument.SelectSingleNode("/configuration/appSettings");

            if (appSettingsNode != null)
            {
                // Remove all previous appSetting nodes
                appSettingsNode.RemoveAll();

                foreach (string key in AppSettings.AllKeys)
                {
                    // Create a new appSetting node
                    XmlElement appSettingNode = myXmlDocument.CreateElement("add");

                    // Create the key attribute and assign its value
                    XmlAttribute keyAttribute = myXmlDocument.CreateAttribute("key");
                    keyAttribute.Value = key;

                    // Create the value attribute and assign its value
                    XmlAttribute valueAttribute = myXmlDocument.CreateAttribute("value");
                    valueAttribute.Value = AppSettings[key];

                    // Append the key and value attribute to the appSetting node
                    appSettingNode.Attributes.Append(keyAttribute);
                    appSettingNode.Attributes.Append(valueAttribute);

                    // Append the appSetting node to the appSettings node
                    appSettingsNode.AppendChild(appSettingNode);
                }
            }

            // Save config file
            myXmlDocument.Save(ConfigurationManager.configFile);
        }

        #endregion
    }
Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 23-May-2008 15:06:57   

Thanks for the feedback.