CLR函数压缩NTEXT类型字段实例讲解

开发 后端
文章主要介绍了一个sql server 2005 使用clr函数压缩ntext类型字段例子,代码清晰,复制一下就可以跑在你的机器上,快来看看吧。

Microsoft开发的CLR被我们所熟悉,怎样能跟好更快的利用clr函数压缩ntext的问题,大家来看看这篇文章吧,相信一定会给你很大的收获。

CLR(公共语言运行库)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集),并保证应用和底层操作系统之间必要的分离。为了提高平台的可靠性,以及为了达到面向事务的电子商务应用所要求的稳定性级别,CLR还要负责其他一些任务,比如监视程序的运行。按照.NET的说法,在CLR监视之下运行的程序属于"受管理的"(managed)代码,而不在CLR之下、直接在裸机上运行的应用或者组件属于"非受管理的"(unmanaged)的代码 。可以在 SQL Server 实例中创建可在 Microsoft .NET Framework 公共语言运行时 (CLR) 中创建的程序集中进行编程的数据库对象。可以充分利用公共语言运行时所提供的丰富的编程模式的数据库对象包括聚合函数、函数、存储过程、触发器以及类型。下面给大家举个sql server 2005 使用clr函数压缩ntext类型字段例子:

vs2005为数据新建一个数据库工程。

using System;  
using System.Data;  
using System.Data.SqlClient;  
using System.Data.SqlTypes;  
using Microsoft.SqlServer.Server;  
using System.IO;  
using System.IO.Compression;  
using System.Text;  
public partial class Gzip  
{  
[Microsoft.SqlServer.Server.SqlFunction]  
public static SqlChars GzipToString(SqlBytes gBytes)  
{  
byte[] bytes = gBytes.Value;  
bytes = Decompress(bytes);  
string str = Encoding.GetEncoding(936).GetString(bytes);  
SqlChars sqlchars = new SqlChars(str);  
return (sqlchars);  
}  
[Microsoft.SqlServer.Server.SqlFunction]  
public static SqlBytes StringToGzip(SqlChars chars)  
{  
byte[] bytes = Encoding.GetEncoding(936).GetBytes(chars.Buffer);  
bytes = Compress(bytes);  
SqlBytes gBytes = new SqlBytes(bytes);  
return (gBytes);  
}  
#region 采用.net系统自带Gzip压缩类进行流压缩  
/// <summary> 
/// 压缩数据  
/// summary> 
/// <param name="data">param> 
/// <returns>returns> 
public static byte[] Compress(byte[] data)  
{  
byte[] bData;  
MemoryStream ms = new MemoryStream();  
GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);  
stream.Write(data, 0, data.Length);  
stream.Close();  
stream.Dispose();  
//必须把stream流关闭才能返回ms流数据,不然数据会不完整  
//并且解压缩方法stream.Read(buffer, 0, buffer.Length)时会返回0  
bData = ms.ToArray();  
ms.Close();  
ms.Dispose();  
return bData;  
}  
/// <summary> 
/// 解压数据  
/// summary> 
/// <param name="data">param> 
/// <returns>returns> 
public static byte[] Decompress(byte[] data)  
{  
byte[] bData;  
MemoryStream ms = new MemoryStream();  
ms.Write(data, 0, data.Length);  
ms.Position = 0;  
GZipStream stream = new GZipStream(ms, CompressionMode.Decompress, true);  
byte[] buffer = new byte[1024];  
MemoryStream temp = new MemoryStream();  
int read = stream.Read(buffer, 0, buffer.Length);  
while (read > 0)  
{  
temp.Write(buffer, 0, read);  
read = stream.Read(buffer, 0, buffer.Length);  
}  
//必须把stream流关闭才能返回ms流数据,不然数据会不完整  
stream.Close();  
stream.Dispose();  
ms.Close();  
ms.Dispose();  
bData = temp.ToArray();  
temp.Close();  
temp.Dispose();  
return bData;  
}  
#endregion  

  • 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.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.

给数据库增加一个varbinary(MAX) 字段,把压缩以后的转移过来以后删除原来的字段。

然后使用clr函数的这两个如下

select:  
SELECT top 10 dbo.GzipToString([content1]) FROM [content_02]  
insert: insert into   [content_02] ([content1]) values(dbo.StringToGzip('123abc'))  
  • 1.
  • 2.
  • 3.

以上是clr函数压缩问题的小例子,快试试吧。

【编辑推荐】

  1. CLR函数实现字符串排序七步通
  2. CLR线程池教程四大功能详解
  3. CLR程序集教程新手上路
  4. 全面解析CLR是什么一点通
  5. 为你解疑C++ CLR和ISO C++原理区别
责任编辑:田树 来源: 博客
相关推荐

2010-09-28 16:22:23

SQL ntext字段

2009-10-23 12:44:35

SQL SERVER

2009-09-17 18:27:40

CLR是什么

2009-09-18 10:40:05

CLR存储过程

2009-10-23 10:50:04

CLR安全性

2009-10-22 16:08:52

.NET CLR是什么

2009-11-23 14:44:22

PHP 5.0构造函数

2012-11-12 09:26:06

.NET多线程

2009-10-23 11:12:21

SQL Server

2009-03-17 16:29:53

SQL ServerCLR.NET Framew

2009-10-23 10:08:29

SQL SERVER

2009-09-18 11:13:09

.Net CLR

2009-10-19 14:25:16

静态构造函数

2009-09-18 09:02:45

CLR Via C#

2010-06-03 18:22:38

Hadoop

2010-09-14 17:20:57

2011-04-02 16:37:26

PAT

2009-10-22 18:41:49

CLR VIA C#教

2010-10-25 11:39:16

oracle函数

2009-08-28 16:37:32

C# for循环
点赞
收藏

51CTO技术栈公众号