Friday, June 03, 2011

在某些特定性能要求下, 希望将动态的asp.net 文件转换成静态文件。

可用以下方式实现:

创建一个类: ResponseGenFileFilter

    public class ResponseGenFileFilter : Stream
    {

        private Stream m_sink;
        private long m_position;
        private FileStream fs;
        public ResponseGenFileFilter(Stream sink,string destPath)
        {
            m_sink = sink;
            fs = new FileStream(destPath, FileMode.Create, FileAccess.Write);
        }
        // The following members of Stream must be overriden.
        public override bool CanRead
        { get { return true; } }
        public override bool CanSeek
        { get { return false; } }
        public override bool CanWrite
        { get { return false; } }
        public override long Length
        { get { return 0; } }
        public override long Position
        {
            get { return m_position; }
            set { m_position = value; }
        }
        public override long Seek(long offset, System.IO.SeekOrigin direction)
        {
            return 0;
        }
        public override void SetLength(long length)
        {
            m_sink.SetLength(length);
        }
        public override void Close()
        {
            m_sink.Close();
            fs.Close();
        }
        public override void Flush()
        {
            m_sink.Flush();
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return m_sink.Read(buffer, offset, count);
        }
        // Override the Write method to filter Response to a file.
        public override void Write(byte[] buffer, int offset, int count)
        {
            //Write out the response to the browser.
            m_sink.Write(buffer, 0, count);
            //Write out the response to the file.
            fs.Write(buffer, 0, count);
        }

    }
然后在ASP.NET 的response 处添加如下代码
Response.Filter = new ResponseGenFileFilter(Response.Filter, genPath);
对ASP.NET 和ASP.NET MVC 都有效
ASP.NET 加在 onInit 函数里面
ASP.NET MVC 加在controller action 方法上。
 
Friday, June 03, 2011 3:23:43 AM (China Standard Time, UTC+08:00)  #    Disclaimer  |   | 
 Thursday, June 02, 2011

有时候需要统计数据库里面的空间占用情况, 用以下SQL实现

SELECT 
TableName = obj.name,  
TotalRows = prt.rows,  
[SpaceUsed(KB)] = SUM(alloc.used_pages)*8  
FROM sys.objects obj  
JOIN sys.indexes idx on obj.object_id = idx.object_id  
JOIN sys.partitions prt on obj.object_id = prt.object_id  
JOIN sys.allocation_units alloc on alloc.container_id = prt.partition_id  
WHERE 
obj.type = 'U' AND idx.index_id IN (0, 1)  
GROUP BY obj.name, prt.rows 
ORDER BY TableName 
Thursday, June 02, 2011 1:54:39 AM (China Standard Time, UTC+08:00)  #    Disclaimer  |   |