62,046
社区成员
发帖
与我相关
我的任务
分享
public static string GetCon()
{
return GetCon("connectionstring");
/*调用web.config中的数据连接*/
}
public static string GetCon(string conn)
/*该函数为继承函数,conn的值继承上函数的返回值connectionstring*/
{
object obj2 = HttpContext.Current.Cache[conn];
/*HttpContext.Current.Cache是将conn的值封装到HttpContext类中,存储在内存中,并赋值给obj2*/
string str = "";
if (obj2 == null)
{
str = WebConfigurationManager.ConnectionStrings[conn].ToString();
/*WebConfigurationManager.ConnectionStrings
命名空间:System.Web.Configuration
程序集:System.Web(位于System.Web.dll)*/
//WebConfigurationManager是处理与Web应用程序相关的配置文件的首选方法。
HttpContext.Current.Cache.Insert(conn, str);
//将str加入到内存中
obj2 = str;
}
//如果obj2的值为空,通过WebConfigurationManager获取数据库连接的方法赋值给str,并把str存入内存,赋值给obj2。
return obj2.ToString();
}
public static string GetCon()
{
return GetCon("connectionstring");
/*调用web.config中的数据连接*/
}
//方法名相同 参数类型或参数数量不同 叫方法的重载 也就是说 你可以 GetCon()执行上面的代码,如果GetCon("xxxx"),则会执行下面的代码
public static string GetCon(string conn)
{
object obj2 = HttpContext.Current.Cache[conn];
/*HttpContext.Current.Cache是将conn的值封装到HttpContext类中,存储在内存中,并赋值给obj2*/
string str = "";
if (obj2 == null)
{
str = WebConfigurationManager.ConnectionStrings[conn].ToString();
/*WebConfigurationManager.ConnectionStrings
命名空间:System.Web.Configuration
程序集:System.Web(位于System.Web.dll)*/
//WebConfigurationManager是处理与Web应用程序相关的配置文件的首选方法。
HttpContext.Current.Cache.Insert(conn, str);
//将str加入到内存中
obj2 = str;
}
//如果obj2的值为空,通过WebConfigurationManager获取数据库连接的方法赋值给str,并把str存入内存,赋值给obj2。
return obj2.ToString();
}
public static string GetCon(string conn = "connectionstring")
而删除第一个方法。实际结果是一样的,并且编译器其实也是生成这样两个方法,然后第1个函数委派调用第2个方法。
从定义上看,这两个函数很显然是不同的。第一个是无参数,那么它执行你们默认约定的行为(比如说等同于输入某个参数)。而第二个要求传入参数,你就可以指定更多更深层次的既定操作。