新聞中心
Microsoft開發(fā)的CLR被我們所熟悉,怎樣能跟好更快的利用clr函數(shù)壓縮ntext的問題,大家來看看這篇文章吧,相信一定會(huì)給你很大的收獲。

成都創(chuàng)新互聯(lián)專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、通化縣網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、商城網(wǎng)站定制開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為通化縣等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
CLR(公共語言運(yùn)行庫(kù))和Java虛擬機(jī)一樣也是一個(gè)運(yùn)行時(shí)環(huán)境,它負(fù)責(zé)資源管理(內(nèi)存分配和垃圾收集),并保證應(yīng)用和底層操作系統(tǒng)之間必要的分離。為了提高平臺(tái)的可靠性,以及為了達(dá)到面向事務(wù)的電子商務(wù)應(yīng)用所要求的穩(wěn)定性級(jí)別,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ù)庫(kù)對(duì)象??梢猿浞掷霉舱Z言運(yùn)行時(shí)所提供的豐富的編程模式的數(shù)據(jù)庫(kù)對(duì)象包括聚合函數(shù)、函數(shù)、存儲(chǔ)過程、觸發(fā)器以及類型。下面給大家舉個(gè)sql server 2005 使用clr函數(shù)壓縮ntext類型字段例子:
vs2005為數(shù)據(jù)新建一個(gè)數(shù)據(jù)庫(kù)工程。
- 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)行流壓縮
- ///
- /// 壓縮數(shù)據(jù)
- /// summary>
- /// name="data"> param>
- ///
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ù)會(huì)不完整
- //并且解壓縮方法stream.Read(buffer, 0, buffer.Length)時(shí)會(huì)返回0
- bData = ms.ToArray();
- ms.Close();
- ms.Dispose();
- return bData;
- }
- ///
- /// 解壓數(shù)據(jù)
- /// summary>
- /// name="data"> param>
- ///
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ù)會(huì)不完整
- stream.Close();
- stream.Dispose();
- ms.Close();
- ms.Dispose();
- bData = temp.ToArray();
- temp.Close();
- temp.Dispose();
- return bData;
- }
- #endregion
- }
給數(shù)據(jù)庫(kù)增加一個(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ù)壓縮問題的小例子,快試試吧。
【編輯推薦】
- CLR函數(shù)實(shí)現(xiàn)字符串排序七步通
- CLR線程池教程四大功能詳解
- CLR程序集教程新手上路
- 全面解析CLR是什么一點(diǎn)通
- 為你解疑C++ CLR和ISO C++原理區(qū)別
網(wǎng)頁名稱:CLR函數(shù)壓縮NTEXT類型字段實(shí)例講解
瀏覽地址:http://m.5511xx.com/article/cogdpde.html


咨詢
建站咨詢
