|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, October 15, 2009 12:32 AM
Points: 14,
Visits: 45
|
|
I am getting Source as XML (more than 5MB size) and It has to compress and store it in DataBase and while getting back from the database, it has to decompress and return the XML data.
Is there any functions available in sql server2005? OR Can I do it in code behind (C#) itself?
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 12:43 AM
Points: 582,
Visits: 1,601
|
|
You should get a certain amount of compression if you use a typed xml column i.e. define a schema for your xml.
It won't be very compressed, but better than nothing. However the xml in output will be equivalent but slightly different (in terms of whitespace and certain other differences.)
Otherwise for real compression which conserves the file as was, you'll have to do it in c# (it seems to me.)
Hope this helps,
David.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:07 AM
Points: 1,221,
Visits: 2,614
|
|
If you do it in code, you could convert the xml to a dataset and use the following compression functions.
http://www.icsharpcode.net/OpenSource/SharpZipLib/ http://www.icsharpcode.net/OpenSource/SD/forum/forum.asp?FORUM_ID=16
Imports ICSharpCode.SharpZipLib.GZip
Imports System.IO
Public Function CompressDataset(ByVal ds As DataSet) Dim BaseStream As System.IO.MemoryStream = New System.IO.MemoryStream Dim OutMemoryStream As System.IO.Stream = New GZipOutputStream(BaseStream) Dim btary() As Byte
ds.WriteXml(OutMemoryStream, XmlWriteMode.WriteSchema) OutMemoryStream.Close() btary = BaseStream.ToArray() BaseStream.Close()
Return btary
End Function
Public Function DecompressDataset(ByVal btary() As Byte)
Dim InStream As Stream = New System.IO.MemoryStream InStream.Write(btary, 0, btary.Length) InStream.Position = 0
Dim CompStream As Stream = New GZipInputStream(InStream) Dim fs As Stream = New System.IO.MemoryStream Dim size As Integer = 2048 Dim writedata As Byte() = New Byte(2048) {}
While True size = CompStream.Read(writedata, 0, size) If size > 0 Then fs.Write(writedata, 0, size) Else Exit While End If End While
fs.Position = 0
Dim ds As New DataSet
ds.ReadXml(fs, XmlReadMode.ReadSchema) CompStream.Close()
fs.Flush() fs.Close() InStream.Close() return ds End Function
End Class
Ken Simmons http://twitter.com/KenSimmons
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, October 15, 2009 12:32 AM
Points: 14,
Visits: 45
|
|
This is fine.
But How can I store this compressed data in Database. Database column either ntext or nvarchar(max).
Doubt is now How to store byte array in Database?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:07 AM
Points: 1,221,
Visits: 2,614
|
|
The byte array is just a string of compressed data. You should be able to store it in a varchar(max) column.
Ken Simmons http://twitter.com/KenSimmons
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855,
Visits: 9,374
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, October 15, 2009 12:32 AM
Points: 14,
Visits: 45
|
|
My doubt how to Convert Byte Array to string
I can not pass directly Byte Array to database. because input to database is string (i.e., nvarchar(max)).
|
|
|
|