关于发布版软件无法加载ReportViewer问题

场景:做RDLC报表开发,开发机可以使用,用户机子上却无法打开

原因:由于做开发的,个人机子上环境比较完整,在做RDLC报表开发时,visual studio 在处理部分dll时,并未将dll复制到本地,即客户机缺少dll,故我们需要手动将其改为复制到本地。

解决方案:

状况1、ReportViewer控件直接无法创建初始化,缺少Microsoft.ReportViewer.Common程序集dll,一般情况,在处理rdlc时,此程序集已经自动引用,只需修改属性“复制到本地”为true;若没有找到,可自行引用,路径如下:

C:\Windows\assembly\GAC_MSIL\Microsoft.ReportViewer.Common\12.0.0.0__89845dcd8080cc91\Microsoft.ReportViewer.Common.dll

其中12.0.0.0__89845dcd8080cc91为版本,尽量和Microsoft.ReportViewer.WinForms版本保持一致。

 

状况2、ReportViewer能够加载,内部rdlc加载错误,提示缺少Microsoft.ReportViewer.ProcessingObjectModel的引用,处理方法同上,dll路径如下:

C:\Windows\assembly\GAC_MSIL\Microsoft.ReportViewer.ProcessingObjectModel\12.0.0.0__89845dcd8080cc91\Microsoft.ReportViewer.ProcessingObjectModel.DLL

常见问题及解决方法

常见问题及解决方法

EF中无法使用时间转字符串

旧地址:https://www.shisujie.com/efdatetostringerror

场景:

查询条件需要使用到时间类型,且需要特殊格式化,例:ToString("yyyy-MM-dd");即,在需要使用时间进行like方式处理时;

此时,用如下方式:

var q = from c in context.HasDateModels where c.UserDate.ToString("yyyy-MM-dd").Contains("20") select c; 

进行查询使用;

提示:LINQ to Entities 不识别方法"System.String ToString(System.String)",因此该方法无法转换为存储表达式

即:Linq to Entities不支持带参数的时间字符串转换

 

解决方法:

以下方法只适用于数据量较小的情况,同时建议,使用此法,若有其他条件,建议先进行一次Linq to Entities筛选后再处理以下情况;

var q = from c in context.HasDateModels.AsEnumerable() where c.UserDate.ToString("yyyy-MM-dd").Contains("20") select c; 

原理:利用AsEnumerable将使用方式转化为Linq to Object,即将数据查询至内存后再进行筛选 

Spring.NET入门(Step1)——基本配置及对象创建

旧地址:http://blog.canself.com/springnet_creatobject/

关于Spring.net及IoC、DI等的介绍,此处就不详述——偶不擅长啦。下面直入主题:首先说说基础配置。

首先是添加引用了,现在微软的nuget操作起来很方便了,直接在nuget找到Spring.Core的项,安装即可,至于其他的Spring.NET相关的dll可以需要时添加使用,如用AOP时,添加Spring.AOP。

下面就是创建对象了:

首先,可以使用最简单的例子,将所有配置都放在app.config中,类及配置文件见下方代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// <summary>
/// 利用App.config配置创建对象
/// </summary>
public class AppConfigObject
{
public AppConfigObject()
{
}

public void Run()
{
Console.WriteLine("AppConfigObject运行!");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!--app.config配置文件-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>

<spring>
<context>
<resource uri="config://spring/objects" />
</context>

<objects xmlns="http://www.springframework.net">
<object id="AppConfigObject" type="SpringNETCreateObject.AppConfigObject, SpringNETCreateObject" />
</objects>

</spring>
</configuration>

使用时如下即可:

1
2
3
IApplicationContext ctx = ContextRegistry.GetContext();
AppConfigObject dao = ctx.GetObject("AppConfigObject") as AppConfigObject;
dao.Run();

但是,软件开发中,为了方便管理,往往添加单独的xml配置。

如:添加单独的Objects.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">

<object id="XMLObject" type="SpringNETCreateObject.XMLObject, SpringNETCreateObject">
</object>
</objects>

然后再app.config的spring/context下添加

1
2
<!--文件复制到输出目录、生成操作为内容时使用file://-->
<resource uri="file://Objects.xml"/>

或者直接在代码里使用xml

1
2
3
IApplicationContext ctx1 = new XmlApplicationContext(
"file://Objects.xml"
);

对象的创建则使用ApplicationContext对象。

最后,讲一些特殊类型的对象创建配置。其中包括嵌入类型、泛型类型。

其中需要在app.config的spring/context下添加

1
2
3
<!--添加resource时,可以自行选择assembly或file,上面用过file了,这里用一下assembly-->
<!--使用assembly时,需要将生成操作修改为嵌入的资源-->
<resource uri="assembly://DAO/DAO/DAOObjects.xml"/>

objects配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">

<object id="normaldao" type="DAO.NormalDAO,DAO"/>
<!--嵌套类型需要使用‘+’连接-->
<object id="nesteddao" type="DAO.NestedDAO+InnerDAO,DAO" />
<!--泛型类型中的小于号需以‘&lt;’替代-->
<object id="genericdao" type="DAO.GenericDAO&lt;int>,DAO" />
</objects>

相关类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <summary>
/// 基类
/// </summary>
public interface BaseDAO
{
void Run();
}

/// <summary>
/// 普通类型
/// </summary>
public class NormalDAO : BaseDAO
{
public void Run()
{
Console.WriteLine("NormalDAO运行");
}
}

/// <summary>
/// 嵌套类型
/// </summary>
public class NestedDAO
{
public class InnerDAO : BaseDAO
{
public void Run()
{
Console.WriteLine("NestedDAO运行");
}
}
}

/// <summary>
/// 泛型类型
/// </summary>
/// <typeparam name="T"></typeparam>
public class GenericDAO<T> : BaseDAO
{
public void Run()
{
Console.WriteLine("泛型({0})运行", this.GetType());
}
}

使用:

1
2
3
4
5
6
7
8
BaseDAO normaldao = ctx.GetObject("normaldao") as BaseDAO;
normaldao.Run();

BaseDAO nesteddao = ctx.GetObject("nesteddao") as BaseDAO;
nesteddao.Run();

BaseDAO genericdao = ctx.GetObject("genericdao") as BaseDAO;
genericdao.Run();

项目地址:https://springnetstudy.codeplex.com

Spring.NET学习资料汇总

旧地址:http://blog.canself.com/spingnet_summary/

相关学习地址:

1、Spring.NET学习笔记——刘冬的博客-Spring.NET分类【强烈推荐,尽管很早之前写的,但真的不错】

2、循序渐进之Spring.Net框架——未细看,不过也是一个系列吧。

3、Spring.NET+NHibernate教程——雨枫技术教程网,页面广告多,内容没细看,不过有完整的流程

4、控制反转,依赖注入介绍——英文,有兴趣看看吧