从简单SQL查询填充C#类
我正在创建一个页面,该页面将显示数据库中审计表的最新记录,我可以使用以下查询获取该记录从简单SQL查询填充C#类,c#,sql,oop,webforms,sql-server-2012,C#,Sql,Oop,Webforms,Sql Server 2012,我正在创建一个页面,该页面将显示数据库中审计表的最新记录,我可以使用以下查询获取该记录 select top 1 StartDateTime, EndDateTime, Status from Audit order by StartDateTime desc 由于此信息将在整个应用程序中多次使用,因此我希望创建一个类,该类将在行中读取并填充一个“stats”对象,我可以从任何页面调用该对象 我的班级现在看起来像 public class Stats { public DateTim
select top 1 StartDateTime, EndDateTime, Status
from Audit
order by StartDateTime desc
由于此信息将在整个应用程序中多次使用,因此我希望创建一个类,该类将在行中读取并填充一个“stats”对象,我可以从任何页面调用该对象
我的班级现在看起来像
public class Stats
{
public DateTime startDate() {
string mySQL;
mySQL = "select top 1 StartDateTime from Audit order by StartDateTime desc";
string myConnectionString;
myConnectionString = "databaseCS";
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[myConnectionString].ConnectionString);
SqlCommand myCommand = new SqlCommand(mySQL, myConnection);
using (myConnection)
{
myConnection.Open();
DateTime startDate = Convert.ToDateTime(myCommand.ExecuteScalar());
return startDate;
}
}
public DateTime endDate()
{
string mySQL;
mySQL = "select top 1 EndDateTime from Audit order by StartDateTime desc";
string myConnectionString;
myConnectionString = "databaseCS";
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[myConnectionString].ConnectionString);
SqlCommand myCommand = new SqlCommand(mySQL, myConnection);
using (myConnection)
{
myConnection.Open();
DateTime endDate = Convert.ToDateTime(myCommand.ExecuteScalar());
return endDate;
}
}
public string status() {
string mySQL;
mySQL = "select top 1 EndDateTime from Audit order by StartDateTime desc";
string myConnectionString;
myConnectionString = "databaseCS";
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[myConnectionString].ConnectionString);
SqlCommand myCommand = new SqlCommand(mySQL, myConnection);
using (myConnection)
{
myConnection.Open();
string status = Convert.ToString(myCommand.ExecuteScalar());
return status;
}
}
显示数据的页面通常至少会显示其中的两个统计信息,因此我希望避免多个数据库查询
是否有一种方法可以更改代码以查询数据库一次
非常感谢在Global.asax的应用程序_start()中,从查询中调用datatable或dataset,然后将其保存到会话
protected void Application_Start()
{
string query = "your query";
///Typically you would have some sort of datalayer
SqlConnection sqlConn = new SqlConnection(conSTR);
sqlConn.Open();
SqlCommand cmd = new SqlCommand(query, sqlConn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
sqlConn.Close();
///Create Stat object
Stats stat = new Stats();
stat.startDate = dt.Columns["startDate"];
//etc
Session["stats"] = stat;
}
然后在页面上
protected void Page_Load(object sender, EventArgs e)
{
Stats stat = Session["stats"] as Stats;
}
一个更可靠的例子
public class MvcApplication : System.Web.HttpApplication
{
//Internal reference to the cache wrapper object
private static ICacheService _internalCacheObject;
//Public mehtod used to inject a new caching service into the application.
// This method is required to ensure full testability.
public void RegisterCacheService(ICacheService cacheService)
{
_internalCacheObject = cacheService;
}
//Use this property to access the underlying cache object from within
// controller methods. Use this instead of native Cache object.
public static ICacheService CacheService
{
get { return _internalCacheObject; }
}
protected void Application_Start()
{
//Inject a global caching service
RegisterCacheService(new AspNetCacheService());
//Store some sample app-wide data
CacheService["StartTime"] = DateTime.Now;
//Call the cache service
// var data = MyNameSpace.MvcApplication.CacheService[...];
}
}
接口
public interface ICacheService
{
Object Get(String key);
void Set(String key, Object data);
Object this[String key] { get; set; }
}
阶级
您如何查询数据库?您需要显示一些内容,或者我们正在猜测。抱歉,现在编辑
“需要多次查询数据库”
-为什么?你的问题中有一个数据库查询,它返回你想要的数据,不是吗?执行该查询一次,就得到了所需的数据。这里到底有什么问题?你是在问如何使用C#中的数据库吗?你是在使用实体框架还是ado.net(sqlClient)@David谢谢你的回答,我说的“多次”是指我想我可以添加代码来查询数据库到每个集合方法中。很多时候,web应用程序将需要所有统计信息,因此需要多次调用,这将导致多个DB查询。你是说我可以把数据库查询放在集合方法之外吗?(如果我使用了错误的术语,我表示歉意,我是一个OOP noob)
public class AspNetCacheService : ICacheService
{
private readonly Cache _aspnetCache;
public AspNetCacheService()
{
if (HttpContext.Current != null)
{
_aspnetCache = HttpContext.Current.Cache;
}
}
public Object Get(String key)
{
return _aspnetCache[key];
}
public void Set(String key, Object data)
{
_aspnetCache[key] = data;
}
public object this[String name]
{
get { return _aspnetCache[name]; }
set { _aspnetCache[name] = value; }
}
}