Tuesday, 27 August 2013

Return string from SQLCLR only 1 character instead of the expected 15

Return string from SQLCLR only 1 character instead of the expected 15

I am getting back from a sql clr function only 1 character when I am
expecting to receive 15.
Here is my sql clr functions:
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic =
true, IsPrecise = true, Name = "usf_Uncompress", SystemDataAccess =
SystemDataAccessKind.None)]
public static SqlBytes usf_Uncompress(SqlBytes compressedBlob)
{
if (compressedBlob.IsNull)
return compressedBlob;
// Preparing to read data from compressed stream
using (var uncompressor = new DeflateStream(compressedBlob.Stream,
CompressionMode.Decompress, true))
{
// Initializing variables
int bytesRead = 1;
const int chunkSize = 50000;
var chunk = new byte[chunkSize];
// Preparing destination stream to hold uncompressed data
var uncompressedData = new MemoryStream();
// Reading from compressed stream
while ((bytesRead = uncompressor.Read(chunk, 0, chunkSize)) > 0)
{
// Writting uncompressed data
uncompressedData.Write(chunk, 0, bytesRead);
}
return new SqlBytes(uncompressedData);
}
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic =
true, IsPrecise = true, Name = "usf_UncompressText",
SystemDataAccess = SystemDataAccessKind.None)]
public static SqlString usf_UncompressText(SqlBytes compressedBlob)
{
if (compressedBlob.IsNull)
return null;
// Prepare to read data from compressed stream
using (var uncompressor = new DeflateStream(compressedBlob.Stream,
CompressionMode.Decompress, true))
{
int bytesRead = 1;
const int chunkSize = 50000;
var chunk = new byte[chunkSize];
// Preparing destination stream to hold uncompressed data
var uncompressedData = new MemoryStream();
// Reading from compressed stream
while ((bytesRead = uncompressor.Read(chunk, 0, chunkSize)) > 0)
{
// Write the uncompressed data to the memory stream
uncompressedData.Write(chunk, 0, bytesRead);
}
uncompressedData.Flush();
// set ms position to start
uncompressedData.Position = 0;
// create streamreader from memorystream
var sr = new StreamReader(uncompressedData);
var text = sr.ReadToEnd();
using (var sw = new StreamWriter("c:\\sqlclr\\junk.txt"))
sw.WriteLine(text);
// Return a new sqlstring object of the text in the
streamreader object
return new SqlString(text);
}
}
I am passing in a compressed varbinary(max) string that I have validated
to be correct already and what I am expecting in the return string is
"test string 123" and what I am getting back is only the "t". Also, stored
in the file for debugging purposes is the correct string value "test
string 123" when looking at the file in a text editor.
when I run the following select statement after installing the clr
functions, I get the following:
SELECT dbo.usf_UncompressText(dbo.usf_CompressText(N'test string 123')),
LEN(dbo.usf_UncompressText(dbo.usf_CompressText(N'test string 123')))
t 30
The 30 is correct because it is a 15 character unicode string or 2 bytes
per character.

No comments:

Post a Comment