yuan8825399 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

working with null(able) dates. what am i missing? #136

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Might just be dense here, but any help would be appreciated.  Does Dapper not 
support/handle DBNull.Value or the ability to set a date in SQL to NULL?

What steps will reproduce the problem?
1.  create mytable table with col mydate that is nullable, add record w/ no date
2.  create mytable class with mydate property as datetime (or date)
3.  load the object using .Query extension method
4.  clear the property using .MyDate = Nothing, or set it using .MyDate = Now
5.  resave the record using .Execute 

What is the expected output? What do you see instead?

Populating the date, the save (Execute) works fine.  Clearing the date using 
.MyDate = Nothing fails on the .Execute.  Also, just not touching an existing 
NULL date from the database fails a save via .Execute as well (i.e. load the 
object, then just save it back w/ no changes).  Similarly, passing an anonymous 
type vs the object also fails, even passing DBNull.Value.  For simplicity, just 
hardcoding id = 1

dim myobject as mytable = connection.Query(Of MyTable)("select * from mytable 
where id = 1") 

connection.Execute("update mytable set mydate = @mydate where id = 
1",myobject,Nothing,Nothing,CommandType.Text)
...or...
connection.Execute("update mytable set mydate = @mydate where id = 1",new with 
{.mydate = DBNull.Value},Nothing,Nothing,CommandType.Text)

What version of the product are you using? On what operating system?
1.12

Please provide any additional information below.

Original issue reported on code.google.com by cchyl...@gmail.com on 16 May 2013 at 8:28

GoogleCodeExporter commented 9 years ago
BTW, I should add.  I _can_ do this w/ dynamic parameters, but the original 
problem actually stemmed from using Dapper.SimpleCRUD to avoid custom logic to 
save objects.  Tracing thru SimpleCRUD led me to the .Execute method they 
(SimpleCRUD) were calling back into Dapper with and looking at how Dapper 
unwinds the obj parameter being passed to the .Execute method after the SQL 
string.  Sorry, should have clarified the purpose here.

Original comment by cchyl...@gmail.com on 16 May 2013 at 8:41

GoogleCodeExporter commented 9 years ago
So, scratch this and feel free to close this issue.  For whatever reason, some 
combination of Nullable(Of T) didn't work the other day.  Perhaps I had my 
datatypes reversed between DateTime and DateTime2.  Anyway, making my date 
property a Nullable(Of DateTime/DateTime2) works just fine.  Sorry if this led 
anyone on a wild goose chase.

Original comment by cchyl...@gmail.com on 21 May 2013 at 4:39

GoogleCodeExporter commented 9 years ago
ok; thanks for the update

Original comment by marc.gravell on 21 May 2013 at 7:17