CLR函數(shù)壓縮NTEXT類型字段實(shí)例講解
Microsoft開發(fā)的CLR被我們所熟悉,怎樣能跟好更快的利用clr函數(shù)壓縮ntext的問題,大家來看看這篇文章吧,相信一定會給你很大的收獲。
CLR(公共語言運(yùn)行庫)和Java虛擬機(jī)一樣也是一個(gè)運(yùn)行時(shí)環(huán)境,它負(fù)責(zé)資源管理(內(nèi)存分配和垃圾收集),并保證應(yīng)用和底層操作系統(tǒng)之間必要的分離。為了提高平臺的可靠性,以及為了達(dá)到面向事務(wù)的電子商務(wù)應(yīng)用所要求的穩(wěn)定性級別,CLR還要負(fù)責(zé)其他一些任務(wù),比如監(jiān)視程序的運(yùn)行。按照.NET的說法,在CLR監(jiān)視之下運(yùn)行的程序?qū)儆?受管理的"(managed)代碼,而不在CLR之下、直接在裸機(jī)上運(yùn)行的應(yīng)用或者組件屬于"非受管理的"(unmanaged)的代碼 ??梢栽?SQL Server 實(shí)例中創(chuàng)建可在 Microsoft .NET Framework 公共語言運(yùn)行時(shí) (CLR) 中創(chuàng)建的程序集中進(jìn)行編程的數(shù)據(jù)庫對象??梢猿浞掷霉舱Z言運(yùn)行時(shí)所提供的豐富的編程模式的數(shù)據(jù)庫對象包括聚合函數(shù)、函數(shù)、存儲過程、觸發(fā)器以及類型。下面給大家舉個(gè)sql server 2005 使用clr函數(shù)壓縮ntext類型字段例子:
vs2005為數(shù)據(jù)新建一個(gè)數(shù)據(jù)庫工程。
- 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系統(tǒng)自帶Gzip壓縮類進(jìn)行流壓縮
- /// <summary>
- /// 壓縮數(shù)據(jù)
- /// < SPAN>summary>
- /// <param name="data">< SPAN>param>
- /// <returns>< SPAN>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流關(guān)閉才能返回ms流數(shù)據(jù),不然數(shù)據(jù)會不完整
- //并且解壓縮方法stream.Read(buffer, 0, buffer.Length)時(shí)會返回0
- bData = ms.ToArray();
- ms.Close();
- ms.Dispose();
- return bData;
- }
- /// <summary>
- /// 解壓數(shù)據(jù)
- /// < SPAN>summary>
- /// <param name="data">< SPAN>param>
- /// <returns>< SPAN>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流關(guān)閉才能返回ms流數(shù)據(jù),不然數(shù)據(jù)會不完整
- stream.Close();
- stream.Dispose();
- ms.Close();
- ms.Dispose();
- bData = temp.ToArray();
- temp.Close();
- temp.Dispose();
- return bData;
- }
- #endregion
- }
給數(shù)據(jù)庫增加一個(gè)varbinary(MAX) 字段,把壓縮以后的轉(zhuǎn)移過來以后刪除原來的字段。
然后使用clr函數(shù)的這兩個(gè)如下
- select:
- SELECT top 10 dbo.GzipToString([content1]) FROM [content_02]
- insert: insert into [content_02] ([content1]) values(dbo.StringToGzip('123abc'))
以上是clr函數(shù)壓縮問題的小例子,快試試吧。
【編輯推薦】