PDA

View Full Version : سوال: تغییر Connection String در App.Config با استفاده از کد داخل برنامه



h.moshfeghi
یک شنبه 12 آبان 1392, 14:32 عصر
یک فایل نمونه از تغییر Connection String در App.Config با استفاده از کد داخل برنامه دارم هر کار کردم نتونستم درستش کنم اگه امکان داره ببینید اشکالش از کجاست.
با تشکر

mohammadsamadi1377
یک شنبه 12 آبان 1392, 15:05 عصر
private static void UpdateConnectionString(string newCS)
{
Configuration config = System.Configuration.ConfigurationManager.OpenExeC onfiguration(
ConfigurationUserLevel.None);
//the full name of the connection string can be found in the app.config file
// in the "name" attribute of the connection string
config.ConnectionStrings.ConnectionStrings["Mosque.Properties.Settings.DatabaseConnectionStrin g"].
ConnectionString = newCS;
//Save to file
config.Save(ConfigurationSaveMode.Modified);

//force changes to take effect so that we can start using
//this new connection string immediately
ConfigurationManager.RefreshSection(config.Connect ionStrings.SectionInformation.Name);
Properties.Settings.Default.Reload();

}

mohammad5593
یک شنبه 12 آبان 1392, 15:18 عصر
سلام ببین خوب شد؟

h.moshfeghi
دوشنبه 13 آبان 1392, 14:48 عصر
سلام ببین خوب شد؟
با تشکر از لطفی که فرمودید.
ولی منظور من این بود که مسیر فایل که مثلا در اینجا درایو C مشخص شده در داخل کانکشن ساخته شده در app.config ذخیره بشه

drstrike
دوشنبه 13 آبان 1392, 21:10 عصر
سلام
فایل Settings.designer.cs رو باز کنید. دسترسی set رو به این شکل (بعد از get ) براش تعریف کنید:

set
{
this["a"] = value;
}


برا تغییر دادن هم به این شکل عمل کنید:

Settings.Default.a = txtDatabase.Text;
Settings.Default.Save();


موفق باشید