Home > Net >  Ini file changes, how to modify the code, to make the values change with the ini file?
Ini file changes, how to modify the code, to make the values change with the ini file?

Time:09-27

 

Public class DBOperate
{
The static string server=Ini. ReadIniData (" database ", "server", null);
The static string database=Ini. ReadIniData (" database ", "database", null);
The static string STR="server=" + + "server; The database="+ database +"; Uid=sa; The Pwd=sml11023 ";
Public SqlConnection conn=new SqlConnection (STR);
}
When the ini file changes, the server and the database because the initialized cannot be changed, could you tell me how to modify to make the server and the database can change with the ini modified?

CodePudding user response:

If ini is a program modification, can be modified to change the database link, if it is on the outside of the program to modify the ini, you need to monitor the change of the ini file to change, specific can search keyword "FileSystemWatcher",

CodePudding user response:

Remove the static, the ini file to read the contents of the cache, and cache depend on, like this:

Public static string ReadContentFromFileToCache (string FullFilePath, string CacheName)
{
Cache Cache=the HttpRuntime. Cache;
String strContent=string. The Empty;
If (cache [CacheName]==null)
{
CacheDependency dp=new CacheDependency (FullFilePath);
The lock (typeof (CacheManage))
{
StrContent=File. ReadAllText (FullFilePath, Encoding UTF8);
}

Cache. Insert (CacheName, strContent, dp);
}
The else
{
StrContent=cache [CacheName]. ToString ();
}

Return strContent;
}

CodePudding user response:

1. Conn originally should be along with pin using (SqlConnection conn=new SqlConnection (STR)) reasonable so as to release the connection pool, so that more concurrent
2. Want to monitoring file changes, please build the Cache file dependence, the baidu yourself "system. C # runtime. The Cache file depends on the Cache", I would not say what
  •  Tags:  
  • C#
  • Related