How can I get the user and password from such a connectionString in the app.config with a .NET function?
Of course I could read that string and get the value after the ID= and Password=.
<connectionStrings> <add name="MyConString" connectionString="Data Source=(local);Initial Catalog=MyDatabase;Persist Security Info=True;User ID=MyUsername Password=MyPassword;Connect providerName="System.Data.SqlClient"/> </connectionStrings>
string database = builder["Initial Catalog"] as string; string UserID = builder["User ID"] as string; string password = builder["Password"] as string; This way you can get all the properties.
Special characters can be escaped by encapsulating them in curly brackets {...}. When there is an un-escaped special character in the ODBC connection string the software will raise an error similar to: "Format of the initialization string does not conform to specification starting at index 90."
use the ConnectionBuilderClass
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder("Your connection string"); string password = builder.Password;
together with the
string connString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString;
to achieve this.
SqlConnectionStringBuilder con = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString); string myUser = con.UserID; string myPass = con.Password;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With