diff --git a/UnitTest/DataSource/DataTableHelper.cs b/UnitTest/DataSource/DataTableHelper.cs new file mode 100644 index 00000000..7b008a10 --- /dev/null +++ b/UnitTest/DataSource/DataTableHelper.cs @@ -0,0 +1,23 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; + +namespace UnitTest.DataSource +{ + [TestClass] + public class DataTableHelper + { + [TestMethod] + public void ToDataTable() + { + var lst = new List(); + var model = new TestEntity.UserModel(); + model.ID = 100; + model.Age = null; + model.Name = "Test"; + lst.Add(model); + var dt = XCLNetTools.DataSource.DataTableHelper.ToDataTable(lst); + Assert.IsTrue(null != dt && dt.Rows.Count == 1 && dt.Rows[0]["ID"].ToString() == "100" && dt.Rows[0]["Age"] == DBNull.Value && dt.Rows[0]["Name"].ToString() == "Test"); + } + } +} \ No newline at end of file diff --git a/UnitTest/Generic/ListHelper.cs b/UnitTest/Generic/ListHelper.cs new file mode 100644 index 00000000..60b179bd --- /dev/null +++ b/UnitTest/Generic/ListHelper.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Data; + +namespace UnitTest.Generic +{ + [TestClass] + public class ListHelper + { + [TestMethod] + public void DataTableToList() + { + var dt = new DataTable(); + dt.Columns.Add("ID", typeof(int)); + dt.Columns.Add("Age", typeof(int)); + dt.Columns.Add("Name", typeof(string)); + var dr = dt.NewRow(); + dr["ID"] = 100; + dr["Age"] = DBNull.Value; + dr["Name"] = "Test"; + dt.Rows.Add(dr); + var lst = XCLNetTools.Generic.ListHelper.DataTableToList(dt); + Assert.IsTrue(null != lst && lst.Count == 1 && lst[0].ID == 100 && lst[0].Age == null && lst[0].Name == "Test"); + } + } +} \ No newline at end of file diff --git a/UnitTest/TestEntity/UserModel.cs b/UnitTest/TestEntity/UserModel.cs new file mode 100644 index 00000000..e1f1567e --- /dev/null +++ b/UnitTest/TestEntity/UserModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UnitTest.TestEntity +{ + public class UserModel + { + public int ID { get; set; } + public int? Age { get; set; } + public string Name { get; set; } + } +} diff --git a/UnitTest/UnitTest.csproj b/UnitTest/UnitTest.csproj index a14a58ae..eb4827c3 100644 --- a/UnitTest/UnitTest.csproj +++ b/UnitTest/UnitTest.csproj @@ -39,6 +39,7 @@ 3.5 + @@ -56,8 +57,11 @@ + + + diff --git a/XCLNetTools/DataSource/DataTableHelper.cs b/XCLNetTools/DataSource/DataTableHelper.cs index ce780182..7a234b49 100644 --- a/XCLNetTools/DataSource/DataTableHelper.cs +++ b/XCLNetTools/DataSource/DataTableHelper.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Data; namespace XCLNetTools.DataSource @@ -66,10 +67,12 @@ public static DataTable ToDataTable(IList data) { System.ComponentModel.PropertyDescriptorCollection properties = System.ComponentModel.TypeDescriptor.GetProperties(typeof(T)); DataTable dt = new DataTable(); + Type nullableType; for (int i = 0; i < properties.Count; i++) { - System.ComponentModel.PropertyDescriptor property = properties[i]; - dt.Columns.Add(property.Name, property.PropertyType); + var property = properties[i]; + nullableType = Nullable.GetUnderlyingType(property.PropertyType); + dt.Columns.Add(property.Name, null == nullableType ? property.PropertyType : nullableType); } object[] values = new object[properties.Count]; foreach (T item in data)