i have a SQL connection in my web.config.
i know how to get it in my .net code but how do i pull out the actual values inside teh connection string like the username, password, and server name?!?!?!?
i have a SQL connection in my web.config.
i know how to get it in my .net code but how do i pull out the actual values inside teh connection string like the username, password, and server name?!?!?!?
string[] arr = ConnectionString.Split(";".ToCharArray());
string server = null, database = null, userid = null, password = null;
foreach (string t in arr)
{
if (t.StartsWith("Data Source="))
{
server = t.Substring(t.LastIndexOf("=") + 1);
}
else if (t.StartsWith("DATABASE="))
{
database = t.Substring(t.LastIndexOf("=") + 1);
}
else if (t.StartsWith("User ID="))
{
userid = t.Substring(t.LastIndexOf("=") + 1);
}
else if (t.StartsWith("Password="))
{
password = t.Substring(t.LastIndexOf("=") + 1);
}
}
Above code should work.
You can use the SqlConnectionStringBuilder for that:
var connectionString = GetConnectionString(); //e.g. use code shown in other answer
var builder = new SqlConnectionStringBuilder(connectionString);
var user = builder.UserID;
There's some sample code on this MSDN page.