Building a connection string from a user's input in a WPF application
If we want in our WPF ap开发者_如何学运维plication to let a user make a SQL Server db connection settings through dialog window, what approach we should use in implementing this.
Should we programmatically make changes to the app.config file? Or, some another approach exists? Or, maybe, in WPF there is some prebuilt tools for doing this?
You could always use one of the built-in dialogs. There is an ADODB connection dialog that requires COM interop. There's also one that has a dependency on a Visual Studio DLL.
But Microsoft has also released the source code to the standard connection dialog available on Code Gallery.
As far as storing the connection string, I would avoid assuming you can write to the App.config unless your application is being installed on a per-user basis. Instead I would store it as a user-scoped setting. If you will allow the user to store the password in the connection string, you should first encrypt the string using the ProtectedData class. Note that you should probably use the per-user scope when encrypting.
Regardless of how you build the connection string, it needs to get into your config file which is supported by ConfigurationManager as long as the user has rights to save the config file in the location where it's being installed.
精彩评论