我在web.config
中定义连接字符串 <ConnectionStrings>
<add name="student" connectionString="Server=student;Max Pool Size=300;Initial Catalog=studentDB;User ID=student;Password=st123dent;" providerName="System.Data.SqlClient"/>
</Connectionstrings>
如何在c#
中动态更改连接字符串答案 0 :(得分:9)
配置是只读的,因此您无法以明显的方式执行此操作,例如
ConfigurationManager.ConnectionStrings["student"].ConnectionString = "new value";
这引发System.Configuration.ConfigurationErrorsException异常,说“配置是只读的”。
这是一个使用反射来重置配置元素的readOnly属性的技巧。 有关详细信息,请参阅此文章Programmatically setting a connectionString property
代码段:
var settings = ConfigurationManager.ConnectionStrings[ 0 ];
var fi = typeof(ConfigurationElement).GetField(
"_bReadOnly",
BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(settings, false);
settings.ConnectionString = "Data Source=Something";
顺便说一句,为什么你需要改变它的配置?我觉得你试图以错误的方式解决一些问题。
答案 1 :(得分:3)
您可以使用WebConfigurationManager.OpenWebConfiguration
或WebConfigurationManager.OpenMappedWebConfiguration
打开Web.Config文件进行读写。如果您具有写入权限,则可以进行修改,例如更改连接字符串。
这肯定要比使用反射修改私有字段更好。
修改web.config然后将回收Web应用程序,因此这不适合让用户更改web.config - 尽管它可以用于特定的场景,例如部署。
示例:
var configurationFileInfo = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
var vdm = new VirtualDirectoryMapping(configurationFileInfo.DirectoryName, true, configurationFileInfo.Name);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
ConnectionStringsSection section = config.GetSection("connectionStrings")
as ConnectionStringsSection;
if (section != null)
{
... modify the section ...
config.Save();
}
答案 2 :(得分:0)
如果用户能够更改设置的值,则web.config文件是存储设置的错误位置。
您应该在设置文件中签出用户范围内的值。
使用这样的设置时,在运行时更改值很简单:
Properties.Settings.Default.ConnectionStringName = "New Connection String";
Properties.Settings.Default.Save();
答案 3 :(得分:0)
使用NameSpace
using System.Configuration;
using System.Web.Configuration;
void ConfigurnewConnectionString(string server, string database, string userid, string password)
{
string str = "server=" + server + ";database=" + database + "; User ID=" + userid + "; Password=" + password + "";
//Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
//str = System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"];
//myConfiguration.Save();
System.Configuration.Configuration Config1 = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection conSetting = (ConnectionStringsSection)Config1.GetSection("connectionStrings");
ConnectionStringSettings StringSettings = new ConnectionStringSettings("conn", "Data Source=" + server + ";Database=" + database + ";User ID=" + userid + ";Password=" + password + ";");
conSetting.ConnectionStrings.Remove(StringSettings);
conSetting.ConnectionStrings.Add(StringSettings);
Config1.Save(ConfigurationSaveMode.Modified);
//Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
//myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text;
//myConfiguration.Save();
}
答案 4 :(得分:0)
放了这段代码
echo urlencode("اوقات-شرعی-جمعه-8-مرداد-ماه-به-اÙÙ‚-اردبیل");
在您的连接中放置此代码
<add name="yourconnectionname" connectionString="Data Source={0};Initial Catalog={1};User ID={2};Password={3}" providerName="System.Data.SqlClient"/>