I have issue with Data in DB (SQL Server). My DB columns has mixture of both Relational data and JSON type data. Few json columns got incorrectly populated. Need C# code to fix the issue.
Example consider my Table name is dbo.Organization.
And columns are
orgReferenceNumber, organizationAssociation.
And organizationAssociation is a JSON type column. Below is the data being populated.
[{"associatedToId":null,"associationType":null,"isObsolete":null}
since all attributes are in null in above json expected output is []
Need C# code which iterates through all records of particular column and checks if all attributes of JSON are empty or NULL. If empty/NULL then just overwrite it to [].
Update: Adding code SSIS C# code Script Component. Below the point where i am facing the issue. object val = objProp.GetValue(value, null);
System.Reflection.TargetParameterCountException HResult=0x8002000E
Message=Parameter count mismatch.
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Reflection;
using Newtonsoft.Json.Linq;
#endregion
namespace ST_cecd1942b9e44e899c1e9e560039f2c3
{
public static class ExtensionMethods
{
public static bool StringPropertiesEmpty(this object value)
{
foreach (PropertyInfo objProp in value.GetType().GetProperties())
{
if (objProp.CanRead)
{
object val = objProp.GetValue(value, null);
if (val.GetType() == typeof(string))
{
if (val == "" || val == null)
{
return true;
}
}
}
}
return false;
}
}
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
System.Data.SqlClient.SqlConnection connection;
ConnectionManager strcon = Dts.Connections["UCP_Q"];
connection = (System.Data.SqlClient.SqlConnection)strcon.AcquireConnection(null);
string sql = "select organisationReferenceNumber,organizationAssociation from dbo.organisation";
SqlCommand cmd = new SqlCommand(sql, connection);
SqlDataReader reader = cmd.ExecuteReader();
long CRN;
String Json;
if (reader.HasRows)
{
while (reader.Read())
{
CRN = Convert.ToInt64(reader.GetValue(0));
Json = Convert.ToString(reader.GetValue(1));
// JObject jObj = JObject.Parse(Json);
bool checkNull = ExtensionMethods.StringPropertiesEmpty(Json.Replace("\"", ""));
//if(checkNull=='true')
//{
//}
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
User contributions licensed under CC BY-SA 3.0