Connection Strings in Code?
Hello,
I added a connection string to Sitefinity by going to Administration --> Settings --> Advanced --> Data --> Connection Strings.
I am creating some custom user controls that I need to interact with a database other than the one Sitefinity uses to store all its data.
How can I access the connection string I created above in code?
Thanks.
Hello Network,
Bellow is some quick sample code which you can use to access connection strings defined in the data configurations:
ConfigManager configManager = Config.GetManager();
configManager.Provider.SuppressSecurityChecks =
true
;
DataConfig dataConfig = configManager.GetSection<DataConfig>();
//get the sitefinity element in the collection
ConnStringSettings Sitefinity = dataConfig.ConnectionStrings[
"Sitefinity"
];
//get the connection string
string
connectionString = Sitefinity.ConnectionString;
PagesConfig pagesConfig = configManager.GetSection<PagesConfig>();