如何更改网站的一些公共数据

 

方法一、使用web.config中的<appSettings>节中的<add key="留言簿名称" value="师生园地留言簿" />保存公共数据,然后使用ReadXml读取或设置公共数据(web.config的内容见1)

相关代码如:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Session("name") <> ConfigurationSettings.AppSettings("站长") Then Response.Redirect("default.aspx")

        If Not IsPostBack Then

            Dim ds As DataSet

            ds = New DataSet()

ds.ReadXml(Server.MapPath("web.config"))

'注意:行从0开始,键值为1列,键名为0列

            s1.Text = ds.Tables(1).Rows(0)(1)

            s2.Text = ds.Tables(1).Rows(3)(1)

            s3.Text = ds.Tables(1).Rows(2)(1)

            s4.Text = ds.Tables(1).Rows(4)(1)

            s5.Text = ds.Tables(1).Rows(6)(1)

            s6.Text = ds.Tables(1).Rows(5)(1)

            s7.Text = ds.Tables(1).Rows(7)(1)

            s8.Text = ds.Tables(1).Rows(8)(1)

            s9.Text = ds.Tables(1).Rows(9)(1)

            s10.Text = ds.Tables(1).Rows(10)(1)

            s11.Text = ds.Tables(1).Rows(11)(1)

            color1.Style.Item("BACKGROUND-COLOR") = ds.Tables(1).Rows(7)(1)

            color2.Style.Item("BACKGROUND-COLOR") = ds.Tables(1).Rows(8)(1)

            color3.Style.Item("BACKGROUND-COLOR") = ds.Tables(1).Rows(9)(1)

            color4.Style.Item("BACKGROUND-COLOR") = ds.Tables(1).Rows(10)(1)

            color5.Style.Item("BACKGROUND-COLOR") = ds.Tables(1).Rows(11)(1)

            If UCase(ds.Tables(1).Rows(12)(1)) = "YES" Then

                s12.SelectedIndex = 0

            Else

                s12.SelectedIndex = 1

            End If

            If UCase(ds.Tables(1).Rows(13)(1)) = "YES" Then

                s14.SelectedIndex = 0

            Else

                s14.SelectedIndex = 1

            End If

            s15.Text = ds.Tables(1).Rows(14)(1)

            s16.Text = ds.Tables(1).Rows(15)(1)

            s17.Text = ds.Tables(1).Rows(16)(1)

'注意清空和释放数据集内的数据;

            ds.Clear()

            ds.Dispose()

        End If

    End Sub

 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim ds As DataSet

        ds = New DataSet()

        ds.ReadXml(Server.MapPath("web.config"))

        ds.Tables(1).Rows(7)(1) = "#000000"

        ds.Tables(1).Rows(8)(1) = "#000000"

        ds.Tables(1).Rows(9)(1) = "#336600"

        ds.Tables(1).Rows(10)(1) = "#0099CC"

        ds.Tables(1).Rows(11)(1) = "#EEECF2"

        ds.Tables(1).Rows(12)(1) = "NO"

        ds.Tables(1).Rows(13)(1) = "YES"

        ds.Tables(1).Rows(14)(1) = "30"

        ds.Tables(1).Rows(15)(1) = "1024"

        ds.Tables(1).Rows(16)(1) = "5"

‘注意以下方法用来接受数据集内的数据改变从而更新数据集内的数据并写入到文件中

        ds.AcceptChanges()

        ds.WriteXml(Server.MapPath("web.config"))

        ds.Clear()

        ds.Dispose()

        Response.Redirect("default.aspx")

    End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ds As DataSet, p(5) As String

        If s12.SelectedItem.Value = "YES" Then

            p(1) = "YES"

        Else

            p(1) = "NO"

        End If

        If s14.SelectedItem.Value = "YES" Then

            p(2) = "YES"

        Else

            p(2) = "NO"

        End If

        If IsNumeric(s15.Text) = False Then

            p(3) = "30"

        Else

            p(3) = s15.Text.ToString

        End If

        If IsNumeric(s16.Text) = False Then

            p(4) = "1024"

        Else

            p(4) = s16.Text.ToString

        End If

        If IsNumeric(s17.Text) = False Then

            p(5) = "5"

        Else

            p(5) = s17.Text.ToString

        End If

        ds = New DataSet()

        ds.ReadXml(Server.MapPath("web.config"))

        ds.Tables(1).Rows(0)(1) = s1.Text

        ds.Tables(1).Rows(3)(1) = s2.Text

        ds.Tables(1).Rows(2)(1) = s3.Text

        ds.Tables(1).Rows(4)(1) = s4.Text

        ds.Tables(1).Rows(5)(1) = s6.Text

        ds.Tables(1).Rows(6)(1) = s5.Text

        ds.Tables(1).Rows(7)(1) = s7.Text

        ds.Tables(1).Rows(8)(1) = s8.Text

        ds.Tables(1).Rows(9)(1) = s9.Text

        ds.Tables(1).Rows(10)(1) = s10.Text

        ds.Tables(1).Rows(11)(1) = s11.Text

        ds.Tables(1).Rows(12)(1) = p(1)

        ds.Tables(1).Rows(13)(1) = p(2)

        ds.Tables(1).Rows(14)(1) = p(3)

        ds.Tables(1).Rows(15)(1) = p(4)

        ds.Tables(1).Rows(16)(1) = p(5)

        ds.AcceptChanges()

        ds.WriteXml(Server.MapPath("web.config"))

        ds.Clear()

        ds.Dispose()

        Response.Redirect("default.aspx")

    End Sub

方法二、用一文件WebAppSettings.Settings这一xml文件保存公共的AppSettings类信息,然后使用AppConfig.cs中的AppConfig类来把公共的设置串行(并行)化AppSettings类。

例程:

WebAppSettings.Settings的内容:
<?xml version="1.0" encoding="utf-8"?>

<Name>111111111</Name>

<Email>000000000</Email>

<Subject>33333333333/Subject></AppSettings>

AppConfig.cs的内容:

using System;

using System.Web;

using System.Xml.Serialization;

using System.Xml;

using System.IO;

using System.Web.Caching;

using System.Text;

 

namespace WebAppSettings

{

     /// <summary>

     /// AppSettings类为一个与配置文件WebAppSettings.Settings中的各个

     /// 属性相对应的对象。

     /// </summary>

    

     public class AppSettings

     {

         private String _name;

         private String _email;

         private String _subject;

 

         public String Name

         {

              get

              {

                   return _name;

              }

              set

              {

                   _name=value;

              }

         }

 

         public String Email

         {

              get

              {

                   return _email;

              }

              set

              {

                   _email=value;

              }

         }

 

         public String Subject

         {

              get

              {

                   return _subject;

              }

              set

              {

                   _subject=value;

              }

         }

 

     }

    

     /// <summary>

     /// AppConfig为专门获取或设置WebAppSettings.Settings中的内容

     /// </summary>

    

     public class AppConfig

     {

         /// <summary>

         /// 根据文件名获得配置内容,filename参数:配置文件名,返回值为一AppSettings对象

         /// </summary>

         public static AppSettings GetSettings(string filename)

         {

              HttpContext context=HttpContext.Current;

              AppSettings data=(AppSettings)context.Cache["AppSettings"];

 

              if(data==null)

              {

                   XmlSerializer serializer=new XmlSerializer(typeof(AppSettings));

                   try

                   {

                       FileStream fs=new FileStream(filename,FileMode.Open);

                       XmlReader reader=new XmlTextReader(fs);

 

                       data=(AppSettings)serializer.Deserialize(reader);

                       reader.Close();

 

                       context.Cache.Insert("AppSettings",data,new CacheDependency(filename));

                   }

                   catch(System.IO.FileNotFoundException)

                   {

                       data=new AppSettings();

                   }

              }

              return data;

         }

 

         /// <summary>

         /// 将对象AppSettings中的内容保存到filename所指的xml文件中保存

         /// </summary>

         public static void SaveSettings(string filename,AppSettings data)

         {

              XmlSerializer serializer=new XmlSerializer(typeof(AppSettings));

              FileStream fs=new FileStream(filename,FileMode.Create);

              XmlTextWriter writer=new XmlTextWriter(fs,new UTF8Encoding());

              serializer.Serialize(writer,data);

              writer.Close();

 

         }

 

     }

}

页面相关代码:

private void Page_Load(object sender, System.EventArgs e)

         {

              // 在此处放置用户代码以初始化页面

              if(!Page.IsPostBack)

              {

                   AppSettings app=AppConfig.GetSettings(Request.PhysicalApplicationPath+"\\WebAppSettings.Settings");

                   this.TextBox1.Text=app.Name;

                   this.TextBox2.Text=app.Email;

                   this.TextBox3.Text=app.Subject;

              }

         }

     private void updata_Click(object sender, System.EventArgs e)

         {

                   AppSettings app=AppConfig.GetSettings(Request.PhysicalApplicationPath+"\\WebAppSettings.Settings");

              app.Name=this.TextBox1.Text;

              app.Email=this.TextBox2.Text;

              app.Subject=this.TextBox3.Text;

         AppConfig.SaveSettings(Request.PhysicalApplicationPath+"\\WebAppSettings.Settings",app);

         }

原文地址:https://www.cnblogs.com/ahuang1118/p/172561.html