林剑锋的博客

专注ASP.NET技术,原创ASP.NET教程

公告栏

QQ群:网站设计师 9908776(已满),设计学院 68075618

用Cache解决ASP.NET频繁读取数据造成的效率问题

数据量的数据变大以后,将造成读取数据耗时问题。如果频繁读取数据库,将造成极大的效率问题困扰。为了建起服务器压力,通过使用Cache机制,可以设定一定的时间间隔,避免服务器压力过大。

Cache机制及用法详解

示例:

if (Cache["mycache1"] == null)
        {
            MyCache mc = new MyCache();
            mc.title = DateTime.Now.ToString();
            Cache.Insert("mycache1", mc, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1));
        }

无法将类型为“System.Data.Common.DataRecordInternal”的对象强制转换为类型“System.Data.DataRowView”

发生这个错误是因为绑定到Repeater的数据源类型的差异。

由于Repeater的DataSource属性可以接受任何System.Collections.IEnumerable 对象,如用于访问数据库的 System.Data.DataViewSystem.Collections.ArrayListSystem.Collections.Hashtable、数组或 IListSource 对象,所以在操作DataItem的转型时,前提是要弄清楚数据源的类型。

如果是把DataTable.Defaultview作为数据源,那么DataItem转型的类型就是DataRowView;

如果是吧DataReader作为数据源,那么DataItem转型的类型就是System.Data.Common.DbDataRecord。

这种转型通常发生在Repeater的DataItemBound处理事件中。

参考网址:

http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.repeater.datasource.aspx Repeater.DataSource属性

http://msdn.microsoft.com/zh-cn/library/system.data.datatable.defaultview(VS.80).aspx DataTable.DefaultView属性

http://forums.asp.net/t/1187140.aspx DataRecordInternal转型

http://zhidao.baidu.com/question/179862651.html 百度贴吧的提问

StringTemplate的主页的服务器居然出故障?

打开www.stringtemplate.org提示:

Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.


--------------------------------------------------------------------------------

Apache/2.2.3 (CentOS) Server at www.stringtemplate.org Port 80

如图:

stringtemplate.jpg

NHibernate发布新版本alpha版!

经过了两年时间的沉默,NHibernate终于发布新版本了!新版本3.0版已经出到Alpha2,相信在不久的将来正式版将会发布。

正在考虑要不要把网站整体迁移到这个框架。有兴趣的朋友赶紧去试试吧。

c#重载构造函数:调用其它构造函数

使用这种方式:

class MyClass{

public MyClass(string people,string word){}

public MyClass(string people):this(people,"hello!"){}

}

使用冒号运算符和this关键字,在声明函数的时候调用其它已存在的构造函数。

StringTemplate自定义日期格式

实现IArributeRenderer接口就可以达到目的下面是来自StackOverFlow的例子。

Use additional renderers like this实现接口的类:

internal class AdvancedDateTimeRenderer : IAttributeRenderer 
{ 
    public string ToString(object o) 
    { 
        return ToString(o, null); 
    } 
 
    public string ToString(object o, string formatName) 
    { 
        if (o == null) 
            return null; 
 
        if (string.IsNullOrEmpty(formatName)) 
            return o.ToString(); 
 
        DateTime dt = Convert.ToDateTime(o); 
 
        return string.Format("{0:" + formatName + "}", dt); 
    } 
} 

and then add this to your StringTemplate such as把类注册到模板组中:

var stg = new StringTemplateGroup("Templates", path); 
stg.RegisterAttributeRenderer(typeof(DateTime), new AdvancedDateTimeRenderer()); 

then in st file模板文件中的写法:

$YourDateVariable; format="dd/mm/yyyy"

验证视图状态 MAC 失败

做了一个页面,有一个用户控件,是用户登录界面;又在主界面加了一个登录界面。

结果老出现这样的错误提示:

验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。

 把用户控件删除,单独一个用户登录功能,还是偶尔会出现。后来找到了下面这篇文章,在web.config中添加如下语句得以解决。

<pages enableEventValidation="false" enableViewStateMac="false" />

 参考网址:http://www.cnblogs.com/sephil/archive/2007/10/19/asp_net_post.html

GDI+ 中发生一般性错误:释放文件句柄

今天在压缩商城的图片是,领略到释放资源的重要性。尤其是在操作文件时,如果不及时释放对文件的访问权,就会导致错误。

在压缩完图片,尝试覆盖原图片时,系统提示:GDI+ 中发生一般性错误。

Google了一下,得到3中可能性:

在开发.NET应用中,使用 System.Drawing.Image.Save 方法而导致“GDI+ 中发生一般性错误”的发生,通常有以下三种原因:
1. 相应的帐户没有写权限。
解决方法:赋予 NETWORK SERVICE 帐户以写权限。
2. 指定的物理路径不存在。
解决方法:
在调用 Save 方法之前,先判断目录是否存在,若不存在,则创建。
if (!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
3. 保存的文件已存在并因某种原因被锁定。
解决方法:
重启IIS,解除锁定。并在代码中使用 using 语句,确保释放 Image 对象所使用的所有资源。

根据猜想,估计是第三种。那么就是资源没有释放啦。在swfupload这个项目的samples中有一个.net生成缩略图的例子,而我,正是照着这个例子来做的。仔细看了它的代码,发现它还有一个释放资源的try...catch...finally语句。

下面是我最后改好的例子:

    private string FixPic2(string pic)
    {
        if (!System.IO.File.Exists(pic))
            return null;

        System.Drawing.Image image = System.Drawing.Image.FromFile(pic);
        if (image != null)
        {
            if (image.Width > 220 || image.Height > 220)
            {
                System.Drawing.Bitmap final_image = null;
                System.Drawing.Graphics graphic = null;
                try
                {
                    System.IO.File.Copy(pic, pic + ".bak", true);

                    int width = image.Width;
                    int height = image.Height;
                    int target_width = 220;
                    int target_height = 220;
                    int new_width, new_height;

                    float target_ratio = (float)target_width / (float)target_height;
                    float image_ratio = (float)width / (float)height;

                    if (target_ratio > image_ratio)
                    {
                        new_height = target_height;
                        new_width = (int)Math.Floor(image_ratio * (float)target_height);
                    }
                    else
                    {
                        new_height = (int)Math.Floor((float)target_width / image_ratio);
                        new_width = target_width;
                    }

                    new_width = new_width > target_width ? target_width : new_width;
                    new_height = new_height > target_height ? target_height : new_height;



                    final_image = new System.Drawing.Bitmap(target_width, target_height);
                    graphic = System.Drawing.Graphics.FromImage(final_image);
                    graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, target_width, target_height));
                    int paste_x = (target_width - new_width) / 2;
                    int paste_y = (target_height - new_height) / 2;
                    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; /* new way */
                    //graphic.DrawImage(thumbnail_image, paste_x, paste_y, new_width, new_height);
                    graphic.DrawImage(image, paste_x, paste_y, new_width, new_height);

                    image.Dispose();
                    //string newFileName = System.IO.Path.GetDirectoryName(Server.MapPath(pic)) + "/" + System.IO.Path.GetFileNameWithoutExtension(pic) + "_s" + System.IO.Path.GetExtension(pic);

                    final_image.Save(pic, System.Drawing.Imaging.ImageFormat.Jpeg);

                    Response.Write("

Fix OK--:" + pic + "

"); Response.Flush(); //return System.IO.Path.GetDirectoryName(pic).Replace("\\", "/") + "/" + System.IO.Path.GetFileNameWithoutExtension(pic) + "_s" + System.IO.Path.GetExtension(pic); } catch (Exception ex) { Response.Write(ex.ToString()); } finally { if (final_image != null) final_image.Dispose(); if (graphic != null) graphic.Dispose(); if (image != null) image.Dispose(); } } } return null; }

 try...catch....finally机制用来释放资源,以保证文件可以在下一次正常调用。

参考链接:http://www.cnblogs.com/wudingfeng/archive/2008/07/24/1250564.html

Sqlite创建数据库文件

使用静态方法:

System.Data.SQLite.SQLiteConnection.CreateFile(filePath)

其中filePath是物理路径+文件名,并且所在文件夹必须存在。

Repeater中的Button不执行OnClick事件?

今天在改一个Asp.net页面,让人奇怪的是,直接放在Form中的Button可以正常执行OnClick事件,而嵌套在Repeater中的Button就不行!

通过排查发现,原来是web.config禁用了ViewState,导致这个问题。解决方法:

通过页面头部的声明中,加入以下语句启用单页面的ViewState:

EnableViewState = True

这样就可以正常执行嵌套在Repeater中的Button事件了。这种情形,一般用于编辑单条数据,比如说删除一条数据。

公告栏

QQ群:网站设计师 9908776(已满),设计学院 68075618

潮州新八景评选活动
超大文件上传
asp.net模板引擎
URL重写组件
« 2010年9月 »
   1234
567891011
12131415161718
19202122232425
2627282930  

统计

文章:157篇
评论:30条 (0条Spam)
相册:1个 (46张图片)
主题:Nagrand主题

CopyRight©2010 LJF.CN. 关于林剑锋