现在正在学习.net 2.0 做了一个自定义表达式,主要是为了解决用access数据库的时候,connectionStrings必须写绝对路径这个很讨厌的问题。
实际运行时没问题,但是在vs2005中设计的时候没办法返回正确的值,所以GridView等设计的时候很不方便,请各位兄弟帮忙。asp.net和C#版各一个,总共200分重谢下面是ExpressionBuilder类
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Configuration;
using System.Web.Compilation;
using System.Collections.Specialized;
using System.CodeDom;namespace Huolx.Library
{
[ExpressionPrefix("DB")]
[ExpressionEditor("DBExpressionBuilderEditor")]
class DBExpressionBuilder: System.Web.Compilation.ExpressionBuilder
{
public override System.CodeDom.CodeExpression GetCodeExpression(System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
string param = entry.Expression;
if (string.Compare(param, "ConnStr", true) == 0)
{
return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings));
}
else if (string.Compare(param, "Type", true) == 0)
{
return new CodePrimitiveExpression(WebConfigurationManager.AppSettings["DataBaseType"]);
} return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings)); } public override object EvaluateExpression(object target, System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
//按照MSDN中的说明,只要实现这个方法,就可以在设计的时候返回计算过的值了,可是……
{
string param = entry.Expression;
if (string.Compare(param, "ConnStr", true) == 0)
{
return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings));
}
else if (string.Compare(param, "Type", true) == 0)
{
return new CodePrimitiveExpression(WebConfigurationManager.AppSettings["DataBaseType"]);
} return bulidConnStr(WebConfigurationManager.AppSettings);
//return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="D:\My Documents\Visual Studio 2005\WebSites\baoming\data\bm.mdb"";
} public override bool SupportsEvaluate
{
get { return true; }
} private string bulidConnStr(NameValueCollection appSettings)
{
string connStr = "";
string _dbType = appSettings["DataBaseType"]; if (String.Compare(_dbType, "ACCESS", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
string dbname = appSettings["DataBaseFilePath"] + appSettings["DataBaseFileName"];
System.Web.UI.Page page = new System.Web.UI.Page(); connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + page.Server.MapPath(dbname) + ";";
}
else if (String.Compare(_dbType, "MSSQL", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
connStr = appSettings["SQL"];
}
return connStr;
}
}\\后面是ExpressionEditor类,翻了半天MSDN,加了这个类还是不管用 [ExpressionPrefix("DB")]
[ExpressionEditor(typeof(Huolx.Library.DBExpressionBuilderEditor))]
public class DBExpressionBuilderEditor : System.Web.UI.Design.ExpressionEditor
{ public override object EvaluateExpression(string expression, object parseTimeData, Type propertyType, IServiceProvider serviceProvider)
{
//string param = expression;
//if (string.Compare(param, "ConnStr", true) == 0)
//{
// return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings));
//}
//else if (string.Compare(param, "Type", true) == 0)
//{
// return new CodePrimitiveExpression(WebConfigurationManager.AppSettings["DataBaseType"]);
//} return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings)); }
private string bulidConnStr(NameValueCollection appSettings)
{
string connStr = "";
string _dbType = appSettings["DataBaseType"]; if (String.Compare(_dbType, "ACCESS", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
string dbname = appSettings["DataBaseFilePath"] + appSettings["DataBaseFileName"];
System.Web.UI.Page page = new System.Web.UI.Page(); connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + page.Server.MapPath(dbname) + ";";
}
else if (String.Compare(_dbType, "MSSQL", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
connStr = appSettings["SQL"];
}
return connStr;
}
}
}
后面是web.config
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<!--=数据库类型可以为"MSSQL"或者"ACCESS"==-->
<add key="DataBaseType" value="ACCESS"/>
<!--=====ACCESS数据库相对路径=================-->
<add key="DataBaseFilePath" value="./App_Data/"/>
<!--=====ACCESS数据库名称=================-->
<add key="DataBaseFileName" value="bm.mdb"/>
<!--=====SQL连接字符串====================-->
<add key="SQL" value="Data Source=.\SQLEXPRESS;Initial Catalog=bmSQL;Integrated Security=True"/>
<!--=======================================-->
<add key="ACCESS" value="=Microsoft.Jet.OLEDB.4.0;Data Source="data\bm.mdb""/>
<!--=======================================-->
</appSettings>
<system.web>
<authentication mode="Forms"/>
<compilation defaultLanguage="c#" debug="true">
<expressionBuilders>
<add expressionPrefix="DB" type="Huolx.Library.DBExpressionBuilder"/>
</expressionBuilders>
<assemblies>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
</system.web>
</configuration>后面是aspx页中引用的部分
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="<%$ DB:ConnStr %>"></asp:Label>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ DB:ConnStr %>" ProviderName="System.Data.OleDb" SelectCommand="SELECT * FROM [peixun]"></asp:SqlDataSource>
实际运行时没问题,但是在vs2005中设计的时候没办法返回正确的值,所以GridView等设计的时候很不方便,请各位兄弟帮忙。asp.net和C#版各一个,总共200分重谢下面是ExpressionBuilder类
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Configuration;
using System.Web.Compilation;
using System.Collections.Specialized;
using System.CodeDom;namespace Huolx.Library
{
[ExpressionPrefix("DB")]
[ExpressionEditor("DBExpressionBuilderEditor")]
class DBExpressionBuilder: System.Web.Compilation.ExpressionBuilder
{
public override System.CodeDom.CodeExpression GetCodeExpression(System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
string param = entry.Expression;
if (string.Compare(param, "ConnStr", true) == 0)
{
return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings));
}
else if (string.Compare(param, "Type", true) == 0)
{
return new CodePrimitiveExpression(WebConfigurationManager.AppSettings["DataBaseType"]);
} return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings)); } public override object EvaluateExpression(object target, System.Web.UI.BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
//按照MSDN中的说明,只要实现这个方法,就可以在设计的时候返回计算过的值了,可是……
{
string param = entry.Expression;
if (string.Compare(param, "ConnStr", true) == 0)
{
return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings));
}
else if (string.Compare(param, "Type", true) == 0)
{
return new CodePrimitiveExpression(WebConfigurationManager.AppSettings["DataBaseType"]);
} return bulidConnStr(WebConfigurationManager.AppSettings);
//return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="D:\My Documents\Visual Studio 2005\WebSites\baoming\data\bm.mdb"";
} public override bool SupportsEvaluate
{
get { return true; }
} private string bulidConnStr(NameValueCollection appSettings)
{
string connStr = "";
string _dbType = appSettings["DataBaseType"]; if (String.Compare(_dbType, "ACCESS", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
string dbname = appSettings["DataBaseFilePath"] + appSettings["DataBaseFileName"];
System.Web.UI.Page page = new System.Web.UI.Page(); connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + page.Server.MapPath(dbname) + ";";
}
else if (String.Compare(_dbType, "MSSQL", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
connStr = appSettings["SQL"];
}
return connStr;
}
}\\后面是ExpressionEditor类,翻了半天MSDN,加了这个类还是不管用 [ExpressionPrefix("DB")]
[ExpressionEditor(typeof(Huolx.Library.DBExpressionBuilderEditor))]
public class DBExpressionBuilderEditor : System.Web.UI.Design.ExpressionEditor
{ public override object EvaluateExpression(string expression, object parseTimeData, Type propertyType, IServiceProvider serviceProvider)
{
//string param = expression;
//if (string.Compare(param, "ConnStr", true) == 0)
//{
// return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings));
//}
//else if (string.Compare(param, "Type", true) == 0)
//{
// return new CodePrimitiveExpression(WebConfigurationManager.AppSettings["DataBaseType"]);
//} return new CodePrimitiveExpression(bulidConnStr(WebConfigurationManager.AppSettings)); }
private string bulidConnStr(NameValueCollection appSettings)
{
string connStr = "";
string _dbType = appSettings["DataBaseType"]; if (String.Compare(_dbType, "ACCESS", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
string dbname = appSettings["DataBaseFilePath"] + appSettings["DataBaseFileName"];
System.Web.UI.Page page = new System.Web.UI.Page(); connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + page.Server.MapPath(dbname) + ";";
}
else if (String.Compare(_dbType, "MSSQL", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
{
connStr = appSettings["SQL"];
}
return connStr;
}
}
}
后面是web.config
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<!--=数据库类型可以为"MSSQL"或者"ACCESS"==-->
<add key="DataBaseType" value="ACCESS"/>
<!--=====ACCESS数据库相对路径=================-->
<add key="DataBaseFilePath" value="./App_Data/"/>
<!--=====ACCESS数据库名称=================-->
<add key="DataBaseFileName" value="bm.mdb"/>
<!--=====SQL连接字符串====================-->
<add key="SQL" value="Data Source=.\SQLEXPRESS;Initial Catalog=bmSQL;Integrated Security=True"/>
<!--=======================================-->
<add key="ACCESS" value="=Microsoft.Jet.OLEDB.4.0;Data Source="data\bm.mdb""/>
<!--=======================================-->
</appSettings>
<system.web>
<authentication mode="Forms"/>
<compilation defaultLanguage="c#" debug="true">
<expressionBuilders>
<add expressionPrefix="DB" type="Huolx.Library.DBExpressionBuilder"/>
</expressionBuilders>
<assemblies>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
</system.web>
</configuration>后面是aspx页中引用的部分
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="<%$ DB:ConnStr %>"></asp:Label>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ DB:ConnStr %>" ProviderName="System.Data.OleDb" SelectCommand="SELECT * FROM [peixun]"></asp:SqlDataSource>
解决方案 »
- 在做聊天服务端时碰见一问题,请大神们帮帮忙。
- 关于对2进制文件的操作
- 高分请教下HttpWebRequest提交数据的问题!!
- 怎么把网页保存到数据库
- 关于取随机数时产生的一个百思不得其解的问题!!!
- 有一个string类型的变量(里面存放的是xml数据),想把这个string变量中内容,存成xml文件,应该怎么做?谢谢
- 问下大侠们,前面是数字结尾允许带空格的正则表达式怎么写呀?
- 关于C#软件打包的问题,请教高手
- 为什么会这样?
- SOS各位阿哥:能否帮我改一下这个24点程序,用面向对象的方法,消除相同的输出字符串表达式,增加程序的可读性.急啊....各位大哥.谢谢拉/....
- C#中指针定义的问题,急!!!
- nunit测试时如何判断不等于、大于、小于的结果?
在config里写上mdb文件从应用程序根开始的路径,如
< ... ConnectionLocation = "\App_Data\xx.mdb"/>在代码里用Request.PhysicalApplicationPath + "读取配置ConnectionLocation"即可