0

I have a wpf application backed up by Entity Framework code first. The application allows user to create new databases and work on them using wpf screens. The creation of a new database is pretty straight forward in Entity Framework code first and is not an issue. However, I am struggling to find a decent way of associating a particular wpf client with current Database he is working on. The database connection string is constructed as given below.

providerConnectionString = "Server=" + serverName + ";" + "Database=" + databaseName +
            "; User Id = userId; Password = password; Persist Security Info=True;";

As you can see above I have to store Server, Database Name, UserId and Password somewhere on client machine so that proper database can be picked up for use when the client starts up the wpf application. To add some more complexity, the user can switch from one database to another if he wishes to using the wpf application. In that case I will have to reset the four connection string parameters stated above to point to new database.

I don't wan't any password protection for the WPF application. Everything written above applies only to SQL Server database connection string.

So in summary, what would be the best way to store the sql server connection string information for a particular wpf client, in an scenario where new databases can be added dynamically from wpf application.

Jatin
  • 4,023
  • 10
  • 60
  • 107

1 Answers1

0

How about storing a default connection in the code and then allowing the user to change the connection details in the WPF application and then storing it in Isolated Storage. Therefore the connection will be per WPF client user. Or if you have to set this in the first instance - check for the logged in user and then find the database connection information and then store it in isolated storage for any subsequent access from the code.

e.g add a key value pair to this object

IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForAssembly();


Dictionary<string,string> dict = new Dictionary<string,string>();

Add to your dictionary...

IsolatedStorageFile isoStore = 
          IsolatedStorageFile.GetStore( IsolatedStorageScope.User 
          | IsolatedStorageScope.Assembly, null, null );
        Stream stream = new 
          IsolatedStorageFileStream(someFileName, 
          FileMode.Create, isoStore );

        if ( stream != null )
        {
            try
            {
                // Serialize the object into the IsolatedStorage.
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize( stream, (Dictionary)dict);
            }
            finally
            {
                stream.Close();
            }
        }
  • That certainly looks promising. The only issue here is that there is no Add(key, value) option available for the IsolatedStorage, which means I have to use standard IO type API to store and update the connection string parameters. – Jatin Dec 17 '12 at 13:00
  • Why not create a dictionary/hashtable, add to it and then serialize.. See edit – The Unculled Badger Dec 17 '12 at 13:25
  • Yes, I thought about these possibilities, and I think instead of using dictionary, I would create a separate Class storing the Connection string parameters and then serialize/deserialize as needed to the IsolatedStorageFile. Thanks – Jatin Dec 17 '12 at 13:50