自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

CLR函數(shù)壓縮NTEXT類型字段實(shí)例講解

開發(fā) 后端
文章主要介紹了一個(gè)sql server 2005 使用clr函數(shù)壓縮ntext類型字段例子,代碼清晰,復(fù)制一下就可以跑在你的機(jī)器上,快來看看吧。

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ù)庫工程。

  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. using System.Data.SqlTypes;  
  5. using Microsoft.SqlServer.Server;  
  6. using System.IO;  
  7. using System.IO.Compression;  
  8. using System.Text;  
  9. public partial class Gzip  
  10. {  
  11. [Microsoft.SqlServer.Server.SqlFunction]  
  12. public static SqlChars GzipToString(SqlBytes gBytes)  
  13. {  
  14. byte[] bytes = gBytes.Value;  
  15. bytes = Decompress(bytes);  
  16. string str = Encoding.GetEncoding(936).GetString(bytes);  
  17. SqlChars sqlchars = new SqlChars(str);  
  18. return (sqlchars);  
  19. }  
  20. [Microsoft.SqlServer.Server.SqlFunction]  
  21. public static SqlBytes StringToGzip(SqlChars chars)  
  22. {  
  23. byte[] bytes = Encoding.GetEncoding(936).GetBytes(chars.Buffer);  
  24. bytes = Compress(bytes);  
  25. SqlBytes gBytes = new SqlBytes(bytes);  
  26. return (gBytes);  
  27. }  
  28. #region 采用.net系統(tǒng)自帶Gzip壓縮類進(jìn)行流壓縮  
  29. /// <summary> 
  30. /// 壓縮數(shù)據(jù)  
  31. /// summary> 
  32. /// <param name="data">param> 
  33. /// <returns>returns> 
  34. public static byte[] Compress(byte[] data)  
  35. {  
  36. byte[] bData;  
  37. MemoryStream ms = new MemoryStream();  
  38. GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);  
  39. stream.Write(data, 0, data.Length);  
  40. stream.Close();  
  41. stream.Dispose();  
  42. //必須把stream流關(guān)閉才能返回ms流數(shù)據(jù),不然數(shù)據(jù)會不完整  
  43. //并且解壓縮方法stream.Read(buffer, 0, buffer.Length)時(shí)會返回0  
  44. bData = ms.ToArray();  
  45. ms.Close();  
  46. ms.Dispose();  
  47. return bData;  
  48. }  
  49. /// <summary> 
  50. /// 解壓數(shù)據(jù)  
  51. /// summary> 
  52. /// <param name="data">param> 
  53. /// <returns>returns> 
  54. public static byte[] Decompress(byte[] data)  
  55. {  
  56. byte[] bData;  
  57. MemoryStream ms = new MemoryStream();  
  58. ms.Write(data, 0, data.Length);  
  59. ms.Position = 0;  
  60. GZipStream stream = new GZipStream(ms, CompressionMode.Decompress, true);  
  61. byte[] buffer = new byte[1024];  
  62. MemoryStream temp = new MemoryStream();  
  63. int read = stream.Read(buffer, 0, buffer.Length);  
  64. while (read > 0)  
  65. {  
  66. temp.Write(buffer, 0, read);  
  67. read = stream.Read(buffer, 0, buffer.Length);  
  68. }  
  69. //必須把stream流關(guān)閉才能返回ms流數(shù)據(jù),不然數(shù)據(jù)會不完整  
  70. stream.Close();  
  71. stream.Dispose();  
  72. ms.Close();  
  73. ms.Dispose();  
  74. bData = temp.ToArray();  
  75. temp.Close();  
  76. temp.Dispose();  
  77. return bData;  
  78. }  
  79. #endregion  

給數(shù)據(jù)庫增加一個(gè)varbinary(MAX) 字段,把壓縮以后的轉(zhuǎn)移過來以后刪除原來的字段。

然后使用clr函數(shù)的這兩個(gè)如下

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

以上是clr函數(shù)壓縮問題的小例子,快試試吧。

【編輯推薦】

  1. CLR函數(shù)實(shí)現(xiàn)字符串排序七步通
  2. CLR線程池教程四大功能詳解
  3. CLR程序集教程新手上路
  4. 全面解析CLR是什么一點(diǎn)通
  5. 為你解疑C++ CLR和ISO C++原理區(qū)別
責(zé)任編輯:田樹 來源: 博客
相關(guān)推薦

2010-09-28 16:22:23

SQL ntext字段

2009-10-23 12:44:35

SQL SERVER

2009-09-17 18:27:40

CLR是什么

2009-10-23 10:50:04

CLR安全性

2009-10-22 16:08:52

.NET CLR是什么

2009-09-18 10:40:05

CLR存儲過程

2012-11-12 09:26:06

.NET多線程

2009-11-23 14:44:22

PHP 5.0構(gòu)造函數(shù)

2009-10-23 11:12:21

SQL Server

2009-09-18 09:02:45

CLR Via C#

2009-09-18 11:13:09

.Net CLR

2009-10-19 14:25:16

靜態(tài)構(gòu)造函數(shù)

2010-06-03 18:22:38

Hadoop

2010-09-14 17:20:57

2011-04-02 16:37:26

PAT

2009-03-17 16:29:53

SQL ServerCLR.NET Framew

2009-10-23 10:08:29

SQL SERVER

2009-10-22 18:41:49

CLR VIA C#教

2010-09-03 10:23:49

PPP Multili

2011-04-01 09:04:09

RIP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號