做网站的时候,global.asax.cs要一直循环运行不停止,程序里面定义了一些数据结构,并且会用网络通信接受一些数据保存在这些结构里面,当有网页访问请求的时候,想调用这些数据,在这些页面对应的cs文件里应该怎样写代码去调用global.asax.cs文件里面的数据呢?

解决方案 »

  1.   

    DEMOusing System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using M_Common.Data;
    using M_Common.Helper;
    using M_Common.Tolerate;/// <summary>
    /// Global 的摘要说明
    /// </summary>    public class Global : System.Web.HttpApplication
        {        protected void Application_Start(object sender, EventArgs e)
            {
                #region 配置默认数据库连接
                string connectionString = "";
                IProvider provider = null;
                switch (ConfigHelper.GetDBType)
                {
                    case "MsSql":
                        connectionString = ConfigHelper.GetconnMsSql;
                        provider = ProviderFactory.CreateProvider(connectionString, "Test", "Test.Data.Separate.SqlServer");
                        break;
                    case "Access":
                        connectionString = ConfigHelper.GetconnMsSql;
                        provider = ProviderFactory.CreateProvider(connectionString, "Test", "-");
                        break;
                    case "Oracle":
                        connectionString = ConfigHelper.GetconnMsSql;
                        provider = ProviderFactory.CreateProvider(connectionString, "Test", "-");
                        break;
                    default:
                        throw new ApplicationException("数据库配置(:连接类型不匹配) 错误!");
                }
                #endregion            ApplicationEnvironments.DefaultProvider = provider;
            }        protected void Application_End(object sender, EventArgs e)
            {        }
        }ProviderFactory  public static IProvider CreateProvider(string connectionString, string assemblyName, string databaseType)
            {
                Assembly assembly = Assembly.Load(assemblyName);
                object db = assembly.CreateInstance(databaseType);
                if (db is AbstractDatabase)
                {
                    return new Provider(connectionString, (AbstractDatabase)db);
                }
                else
                {
                    throw new InvalidCastException("config文件中定义的程序集没有继承AbstractConfigContext!");
                }
            }得到一个数据库对象