zoukankan      html  css  js  c++  java
  • 转:ASP.Net下两种全局变量的设置和读取方法

     
    本文介绍两种ASP.Net项目中全局变量使用的方式。web.config文件 和 Gloab文件。以下分别说明:

    方法一:web.config文件
    ——设置:
    在web.config文件里添加关键字key是通过<appSettings>标记来实现的,但是appSettings标记通常放在<system.web>.....</system.web>标记外面。例:
    <configration>
    <appSettings>
    <add key="connString1" value="server=localhost;user id=sa;pwd=;database=数据库名字"/>
    <add key="connString2" value="provider=Microsoft.Jet.OleDb.4.0;Data Source=数据库路径"/>
    </appSettings>
    <system.web>
    </system.web>
    </configration>

    ——读取:
    要在代码中引用这些数据库连接字符串,需要先添加对System.ConFiguration名字空间的引用,在这个名字空间中含有ConfigurationSettings类,其静态方法ConfigurationSettings.AppSettings属性可获取web.config文件中<appSettings>节的设置,读到的值为string型。例如:

    using System.Configuration;
    string conn1 = ConfigurationSettings.AppSettings["connString1"];
    string conn2 = ConfigurationSettings.AppSettings["connString2"];
    SQLConnection myConn1 = new SQLConnection(conn1);
    OleDbConnection myConn2 = new OleDbConnection(conn2);

    在VS2005中, ConfigurationSettings.AppSettings 可以换成 ConfigurationManager.AppSettings

    方法二:Gloab文件
     ——设置:
    在Global文件里中添加
    protected void Session_Start(Object sender, EventArgs e)
    {
    Session["sqlConnectionString"] = "uid=Username;pwd=password;database=MyTest;server=Localhost;Connect Timeout=300";
    }

    ——读取:
    在代码中的应用:
    String strConnection=Session["sqlConnectionString"].ToString();
    sqlConnection_1=new SqlConnection(strConnection);
  • 相关阅读:
    验证字符串空“” 的表达式
    should not this be a private conversation
    【转】你真的了解word-wrap 和 word-break 的区别吗
    To live is to function,that is all there is in living
    [转] windows 上用程序putty使用 ssh自动登录Linux(Ubuntu)
    Vim/gvim容易忘记的快捷键
    [转] Gvim for windows中块选择的方法
    问题集
    web services
    Trouble Shooting
  • 原文地址:https://www.cnblogs.com/zhangzheny/p/650216.html
Copyright © 2011-2022 走看看