diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..f8b4888 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": null +} \ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..f584f9f Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/Logic/Common.cs b/Logic/Common.cs index fc1ef2d..c4486b2 100644 --- a/Logic/Common.cs +++ b/Logic/Common.cs @@ -1,3 +1,5 @@ +using Newtonsoft.Json; +using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; @@ -14,7 +16,17 @@ public static DataTable GetDataTableFromFields(IEnumerable data, SqlBulkCo foreach (PropertyInfo propertyInfo in listType.GetProperties()) { var columnName = GetColumnName(propertyInfo); - dt.Columns.Add(columnName, propertyInfo.PropertyType); + + if (propertyInfo.PropertyType.IsNonStringEnumerable()) + { + dt.Columns.Add(columnName, typeof(string)); + } + else + { + dt.Columns.Add(columnName, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType); + } + + sqlBulkCopy.ColumnMappings.Add(columnName, columnName); } @@ -24,7 +36,15 @@ public static DataTable GetDataTableFromFields(IEnumerable data, SqlBulkCo foreach (PropertyInfo propertyInfo in listType.GetProperties()) { var columnName = GetColumnName(propertyInfo); - dr[columnName] = propertyInfo.GetValue(value, null); + + if (propertyInfo.PropertyType.IsNonStringEnumerable()) + { + dr[columnName] = JsonConvert.SerializeObject(propertyInfo.GetValue(value, null)); + } + else + { + dr[columnName] = propertyInfo.GetValue(value, null) ?? DBNull.Value; + } } dt.Rows.Add(dr); } diff --git a/Logic/Extensions.cs b/Logic/Extensions.cs new file mode 100644 index 0000000..bac96d1 --- /dev/null +++ b/Logic/Extensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +public static class Extensions +{ + public static bool IsNonStringEnumerable(this PropertyInfo pi) + { + return pi != null && pi.PropertyType.IsNonStringEnumerable(); + } + + public static bool IsNonStringEnumerable(this object instance) + { + return instance != null && instance.GetType().IsNonStringEnumerable(); + } + + public static bool IsNonStringEnumerable(this Type type) + { + if (type == null || type == typeof(string)) + return false; + return typeof(IEnumerable).IsAssignableFrom(type); + } +} \ No newline at end of file diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 31343a6..46d3f3d 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -30,6 +30,9 @@ 4 + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + @@ -41,10 +44,14 @@ + + + +