web. Config 中 Appsettings 应用技巧

来源:互联网 发布:淘宝注册开网店的步骤 编辑:程序博客网 时间:2024/06/05 11:30

 
基本语法:

---------------------------------------------------------------------------

设置:

<appSettings>

<add key="keywordname" value="" />

</appSettings>

调用:(VB)

<%=ConfigurationSettings.AppSettings("keywordname")%>

Dim keyword As String = ConfigurationSettings.AppSettings("keywordname")

 

 一、设置环境变量

---------------------------------------------------------------------------

设置:

在web. Config中加入,

<configuration>

<appSettings>

<add key="title" value="渔夫之家" />

<add key="keyword" value="软件下载, 文章阅读, 技术文章, 搜索引擎" />

<add key="description" value="软件下载, 文章阅读, 技术文章, 搜索引擎" />

<add key="aboutland" value="... Our Network" />

<add key="softsqlconnstring" value="server=127.0.0.1;database=blog;uid=sa;pwd=123456"/>

<add key="indexdown" value="14"/>

<add key="indexinfo" value="8"/>

<add key="indexread" value="10"/>

<add key="indexhits" value="10"/>

<add key="indexpic" value="3"/>

<add key="indexclass" value="10"/>

<add key="indexall" value="国产软件,国外软件,汉化补丁"/>

<add key="infoindex" value="10"/>

<add key="downindex" value="10"/>

<add key="fs" value="0"/>

</appSettings>

 

 <system.web>

  ......

 </system.web>

</configuration>

 

 

调用:(VB)

可生成 用户控件 .ascx 文件

<title><%=ConfigurationSettings.AppSettings("title")%></title>

<meta name="keywords" content="<%=ConfigurationSettings.AppSettings("keyword")%>">

<meta name="description" content="<%=ConfigurationSettings.AppSettings("description")%>">

<meta name="about:land" content="<%=ConfigurationSettings.AppSettings("aboutland")%>">

 

 

二、数据库连接

---------------------------------------------------------------------------

ACCESS

-----------------------------------

设置:

在web. Config中加入,

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="ConnectionString" value="Provider=Microsoft.jet.oledb.4.0;data source=D:/web code/aspnettest/webconfig/access/data/)#data.mdb;" />

</appSettings>

<system.web>

<compilation debug="true"/>

</system.web>

</configuration>

 

调用:(VB)

<script language="vb" runat="server">

Dim dsn As String = ConfigurationSettings.AppSettings("ConnectionString")

 

Sub Page_Load(send As Object, E As EventArgs)

If Not IsPostBack Then

OpenDataBase_And_BindToDataGrid()

End If

 

End Sub

 

Sub ChangePage(sender As Object, E As  DataGridPageChangedEventArgs)

MyGrid.CurrentPageIndex = E.NewPageIndex

OpenDataBase_And_BindToDataGrid()

End Sub

 

Sub OpenDataBase_And_BindToDataGrid()

 

Dim Conn As OleDbConnection, Adpt As OleDbDataAdapter

Dim Ds As DataSet, SQL As String, I As Integer

Dim Table1 As DataTable

Conn = New OleDbConnection(dsn)

Conn.Open()

 

 

 

SQL = "Select * From BBS_TITLES Order By Titles_LASTPOSTDATE Desc"

Adpt = New OleDbDataAdapter( SQL, Conn )

Ds = New DataSet()

Adpt.Fill(Ds, "BBS_TITLES")

Table1 = Ds.Tables( "BBS_TITLES" )

Table1.Columns.Add(New DataColumn("TITLES_DATE", GetType(String)))

For I = 0 To Table1.Rows.Count-1

Dim D1 As Date = Table1.Rows(I).Item("TITLES_CREATEDATE")

Dim D2 As Date = Table1.Rows(I).Item("TITLES_LASTPOSTDATE")

Table1.Rows(I).Item("TITLES_DATE") = Format( D1, "MM/dd") & "-" & Format( D2, "MM/dd")

Next

 

MyGrid.DataSource = Table1.DefaultView

MyGrid.DataBind()

 

Conn.Close()

 

End Sub

   </script>

 

SQL Server

-----------------------------------

设置:

在web. Config中加入,

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="pubs2" value="uid=sa;password=test;database=pubs;server=(local)" />

<add key="pubs" value="uid=sa;password=test;database=pubs;server=web" />

</appSettings>

<system.web>

<compilation debug="true"/>

</system.web>

</configuration>

 

 

调用:(VB)

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.Configuration" %>

<html>

<script language="VB" runat="server">

Sub Page_Load(Src As Object, E As EventArgs)

Dim dsn As String = ConfigurationSettings.AppSettings("pubs")

Dim MyConnection As SqlConnection

Dim MyCommand As SqlDataAdapter

 

MyConnection = New SqlConnection(DSN)

MyCommand = New SqlDataAdapter("select * from Authors", MyConnection)

 

Dim DS As New DataSet

MyCommand.Fill(DS, "作者")

 

MyDataGrid.DataSource= New DataView(DS.Tables(0))

MyDataGrid.DataBind()

End Sub

</script>

<body>

<h3><font face="宋体">数据</font></h3>

<ASP:DataGrid id="MyDataGrid" runat="server"

BackColor="#ccccff"

BorderColor="black"

ShowFooter="false"

CellPadding=3

CellSpacing="0"

Font-Name="宋体"

Font-Size="8pt"

HeaderStyle-BackColor="#aaaadd"

/>

</body>

</html>