diff --git a/table.lib.tests/SqlInsertTests.cs b/table.lib.tests/SqlInsertTests.cs index d46334e..e2fdac6 100644 --- a/table.lib.tests/SqlInsertTests.cs +++ b/table.lib.tests/SqlInsertTests.cs @@ -1,4 +1,8 @@ +using Dapper; using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Linq; using table.runner; namespace table.lib.tests @@ -31,7 +35,33 @@ public void TestNullGeneration() var lines = s.Split(Environment.NewLine); Assert.Multiple(() => { - Assert.That(lines[0], Is.EqualTo("INSERT INTO TestClass (Field1,Field2,Field3,Field4,Field5,Field6) VALUES (321121,NULL,2121.32,1,'1970-01-01',34.43);")); + Assert.That(lines[0], Is.EqualTo("INSERT INTO TestClass (Field1,Field2,Field3,Field4,Field5,Field6) VALUES (NULL,NULL,NULL,NULL,NULL,NULL);")); + }); + } + + [Test] + public void TestNullFromDBGeneration() + { + IEnumerable> table; + using (var connection = new SqlConnection(@"Data Source=DESKTOP-TTUSQLJ\SQLEXPRESS;Initial Catalog=store;Integrated Security=True")) + { + connection.Open(); + const string data = @" +SELECT PhoneNumber, LockoutEnd + FROM [store].[dbo].[AspNetUsers] +"; + table = connection.Query(data) as IEnumerable>; + } + + var enumerable = table as IDictionary[] ?? + (table ?? throw new InvalidOperationException()).ToArray(); + + + var s = DbTable.Add(enumerable).ToSqlInsertString(); + var lines = s.Split(Environment.NewLine); + Assert.Multiple(() => + { + Assert.That(lines[0], Is.EqualTo("INSERT INTO Table1 (PhoneNumber,LockoutEnd) VALUES (NULL,NULL);")); }); } } diff --git a/table.lib/DbTable.cs b/table.lib/DbTable.cs index 3c3a511..3942b43 100644 --- a/table.lib/DbTable.cs +++ b/table.lib/DbTable.cs @@ -238,7 +238,7 @@ public string ToSqlInsertString() DateTime time => "'" + time.ToString("yyyy-MM-dd") + "'", decimal value1 => value1.ToString("#0.0###"), double value1 => value1.ToString("#0.0###"), - _ => (obj != null ? obj.ToString().ToSql() : "") + _ => (obj != null ? obj.ToString().ToSql() : "NULL") }; s += $"{p},"; } diff --git a/table.lib/table.lib.csproj b/table.lib/table.lib.csproj index 4c3efc4..cbf5b10 100644 --- a/table.lib/table.lib.csproj +++ b/table.lib/table.lib.csproj @@ -3,17 +3,17 @@ net6.0 table.lib - 1.12.0 + 1.13.0 Jordi Corbilla LICENSE https://github.com/JordiCorbilla/table.lib - v1.12 + v1.13 - Upgrade to .net 6 true true Simple c# (.NET 6) table library that renders any List<T> or Dictionary<TV, T> into a nicely formatted markdown, csv, html, specflow or console table, allowing for extra formats. It also supports dynamic returns from Dapper as IEnumerable<IDictionary<string, object>> via DBTable object. - 1.12.0.0 - 1.12.0.0 + 1.13.0.0 + 1.13.0.0 diff --git a/table.runner/Samples.cs b/table.runner/Samples.cs index 7426cf4..6a2f3d2 100644 --- a/table.runner/Samples.cs +++ b/table.runner/Samples.cs @@ -80,8 +80,8 @@ public static List GetNullOutput() { new TestClass { - Field1 = 321121, Field2 = null, Field3 = 2121.32m, Field4 = true, - Field5 = new DateTime(1970, 1, 1), Field6 = 34.43 + Field1 = null, Field2 = null, Field3 = null, Field4 = null, + Field5 = null, Field6 = null } }; return list; diff --git a/table.runner/TestClass.cs b/table.runner/TestClass.cs index b58e49d..5710781 100644 --- a/table.runner/TestClass.cs +++ b/table.runner/TestClass.cs @@ -26,11 +26,11 @@ namespace table.runner { public class TestClass { - public int Field1 { get; set; } - public string Field2 { get; set; } - public decimal Field3 { get; set; } - public bool Field4 { get; set; } - public DateTime Field5 { get; set; } - public double Field6 { get; set; } + public int? Field1 { get; set; } + public string? Field2 { get; set; } + public decimal? Field3 { get; set; } + public bool? Field4 { get; set; } + public DateTime? Field5 { get; set; } + public double? Field6 { get; set; } } } \ No newline at end of file