📄 ratingrecord.cs
字号:
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
/// <summary>
///评价记录
/// </summary>
public class RatingRecord
{
// 根目录
private string _ApplicationPath;
private DataSet _ds;//数据源
public RatingRecord()
{
}
private DataTable Table//所有的评价记录
{
get
{
if (_ds == null)//数据源为空,则从xml文件中读取
{
_ds = new DataSet();
_ds.ReadXml(XmlFile);
}
return _ds.Tables[0];
}
}
//获取网站的跟目录
protected string RootPath
{
get
{
if (_ApplicationPath != null)
{
return _ApplicationPath;
}
else if (HttpContext.Current != null)//获取根目录
{
return _ApplicationPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
}
else//出现错误
{
throw new ApplicationException("没有找到根目录.");
}
}
}
protected string XmlFile //xml文件的名字,包含路径
{
get { return Path.Combine(RootPath, @"App_Data\RatingRecord.xml"); }
}
public string Count(string clas, string stars)//获取某种水果,特定星级(比如3星级)的评价个数
{
Table.DefaultView.RowFilter = "class='" + clas + "' and stars='" + stars + "'";
return Table.DefaultView.Count.ToString();//返回个数
}
public void Insert(string clas, string stars)//插入一项任务
{
DataRow dr = Table.NewRow();//新的一行
dr["id"] = Table.Rows.Count;//标志
dr["class"] = clas;//被评价的水果名称
dr["stars"] = stars;//星级
dr["time"] = DateTime.Now.ToLocalTime();//评价时间
Table.Rows.Add(dr);
SaveChanges();//保存插入结果
}
protected void SaveChanges()//保存xml文件
{
_ds.WriteXml(XmlFile, XmlWriteMode.IgnoreSchema);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -