Hi Team,
When am using bellow script in SSIS script task am getting bellow. Please help how can i use script task to execute bellow provided C# code.
Error:
WarningCA2202Object 'msi' can be disposed more than once in method 'Program.Unzip(byte[])'.
To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.
My C# code:
#region Namespacesusing System;
using System.Data.SqlClient;
using System.IO;
using System.IO.Compression;
using System.Text;
#endregion
namespace UnZipData
{
class Program
{
static void Main(string[] args)
{
SqlConnection con;
SqlDataReader reader;
try
{
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Profiler"].ConnectionString;
con = new SqlConnection(connection);
con.Open();
reader = new SqlCommand("SELECT TOP 100 Id,Name,requestXML,responseXML FROM TableName order by 1 desc", con).ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file. You do NOT need to call Flush() or Close().
string filePath = $@"D:\FareRules\" + reader.GetInt32(0);
File.WriteAllText(filePath, Unzip((byte[])reader["requestXML"]));
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void CopyTo(Stream src, Stream dest)
{
byte[] bytes = new byte[4096];
int cnt;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
{
dest.Write(bytes, 0, cnt);
}
}
public static string Unzip(byte[] bytes)
{
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
//gs.CopyTo(mso);
CopyTo(gs, mso);
}
return Encoding.UTF8.GetString(mso.ToArray());
}
}
}
}
Thanks Bala Narasimha