我只是在表中使用[c#,DAPPER,MS Access]插入数据,但是c#中[DateTime]的列被映射到MsAccess中的[date / time]列会导致类似“条件表达式中的数据不匹配”之类的问题。
经过长时间的研究,我得到了一个link,但“地板”解决方案在这里不起作用。任何建议都是最受欢迎的。
我找到的最佳解决方案是将查询字符串中的参数语法从@Foo
更改为?Foo?
。
试试这样对我有用的东西:
@CLStart = DateTime.Parse(Convert.ToDateTime(client.CLStart).ToShortDateString())
client.CLStart
这里是数据类型DateTime?
- Nullable<DateTime>
在查询具有多个参数时,在与Dapper和MS Access一起手写SQL查询之前,我遇到过这个问题。问题是访问引擎不遵守参数顺序并按字母顺序对它们进行排序。
以这个名为MyTable的示例表为例:
MyNumber Number
MyDate Date/Time
假设你有一个C#类MyClass:
public class MyClass
{
public int MyNumber { get; set; }
public DateTime MyDate { get; set; }
}
并将一个实例myClass传递给以下Dapper语句:
connection.Execute("INSERT INTO MyTable (MyNumber, MyDate) VALUES (@MyNumber, @MyDate)", myClass);
由于System.Data.OleDb.OleDbException:'条件表达式中的数据类型不匹配',该语句将失败。
这是因为Access将按字母顺序对参数进行排序,在这种情况下,它会尝试将数字粘贴到日期中,将日期粘贴到数字中。
您可以通过以字母顺序友好的方式命名参数或按字母顺序排序来解决此问题。在这种情况下,上述声明可以改写为:
connection.Execute("INSERT INTO MyTable (MyDate, MyNumber) VALUES (@MyDate, @MyNumber)", myClass);
我在vb.net中遇到了同样的问题,我就这样解决了。
Public Shared Function dapperBugDate(ByVal birthday As Date)
birthday = birthday.ToString("yyyy-MM-dd HH:mm:ss")
Return birthday
End Function
Sub save()
Dim actions = db.Execute("INSERT into table (birthday) VALUE (@birthday)", New table With {.birthday = dapperBugDate(Me.birthday)})
End Sub