ASP.NET数据访问结构类
类的实现如下,执行到string assemblyFilePath =page.Request.ApplicationPath的时候出现错误提示System.Web.HttpException: 请求在此上下文中不可用,请问如何解决。
using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.WebControls;
namespace DAL
{
public class ConnectionString:System.Web.UI.Page
{
/// <summary>
/// 获取数据链接地址
/// </summary>
/// <returns></returns>
public static string GetConnection()
{
System.Web.UI.Page page=new System.Web.UI.Page();
string assemblyFilePath =page.Request.ApplicationPath;
string configFilePath = page.MapPath(assemblyFilePath).Replace("/","\\") + "\\AppConfig.config";
// string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
// string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
// string configFilePath = assemblyDirPath + "\\AppConfig.config";
Sailun.SqlServerDal.Config config = new Config(configFilePath);
//数据服务器
string dataserver = config.GetValue("RemoteSQLServerUri");
//数据服务器用户名称
string user = config.GetValue("RemoteSQLServerUser");
//数据服务器密码
string pwd = config.GetValue("RemoteSQLServerPWD");
//采用的数据库
string database=config.GetValue("RemoteSQLDataBase");
return "data source="+dataserver+"; user id="+user+"; initial catalog="+database+";password="+pwd;
}
}
}