像这种.cs文件里直接读取数据库字段的值,在这里,给大家说一下它是怎样的原理。
int BuyPV = Convert.ToInt32(DssHelper.GetValueByApplicationString("System_Member_Level_PV", UserLevel));
这是截取字符串的方法。
public static string GetValueByApplicationString(string ApplicationStr, int i)
{
string str = "0";
if (((i.ToString().Length != 0) && (i != 0)) && (HttpContext.Current.Application[ApplicationStr] != null))
{
string[] strArray = HttpContext.Current.Application[ApplicationStr].ToString().Split(new char[] { ',' });
if (strArray.Length >= i)
{
str = strArray[Convert.ToInt32(i) - 1];
}
}
return str;
}
Application 作为全局变量,类似于session .cookie。它是始于程序开始时,结束于程序结束时。
Global.asax 文件是影响全局类的一个文件。
<%@ Application Language="C#" %>
<%@ import namespace="CS.Business.Common.Config" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
DssConstants.Initialization();
Application["user_sessions"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不引发该事件。
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] - 1;
Application.UnLock();
}
</script>
然后打开 在应用程序启动时运行的代码 Initialization()方法
if (System.Web.HttpContext.Current.Application["Initialization"] == null || System.Web.HttpContext.Current.Application["Initialization"].ToString() != "True")
{
int s1 = crud.QueryTable(" SELECT * FROM information_schema.COLUMNS WHERE TABLE_Name = 'DS_Config' ").Rows.Count;
int s2 = crud.QueryTable(" SELECT * FROM information_schema.COLUMNS WHERE TABLE_Name = 'DS_Config' AND COLUMN_NAME = 'Para' ").Rows.Count;
int s3 = crud.QueryTable(" SELECT * FROM information_schema.COLUMNS WHERE TABLE_Name = 'DS_Config' AND COLUMN_NAME = 'C_Value_Cn' ").Rows.Count;
if (s1 > 0)
{
System.Web.HttpContext.Current.Application.Remove("Initialization");
System.Web.HttpContext.Current.Application.Remove("Count");
dt = crud.SelectRecordByPara("DS_Config", "C_Flag", "Integer", "1", "ID", "desc");
for (i = 0; i < dt.Rows.Count; i++)
{
try
{
try
{
if ((s2 > 0) && (s3 > 0))
{
System.Web.HttpContext.Current.Application.Remove(dt.Rows[i]["Para"].ToString());
System.Web.HttpContext.Current.Application.Add(dt.Rows[i]["Para"].ToString(), dt.Rows[i]["C_Value_Cn"].ToString());
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
finally
{
}
}
}
在这里就可以看到它是读取的哪个数据表的字段。