我在使用"Date“属性时遇到了问题。我的类在这里:
public bool ChuyenDSChamCong()
{
try
{
DataTable dta = DSChamCong();
if (dta == null)
return false;
foreach (DataRow r in dta.Rows)
{
try
{
string sql = "delete FROM tk_dlchamcong where MD5(CONCAT(ma_chamcong,ma_nv, tg_check)) = md5('" + r["CardID"] + r["StaffID"] + r["TransDT"].ToString() + "')";
MYSQLDB.query(sql);
while (((DateTime)r["TransDT"]).Date == (DateTime.Today).Date)
{
string sql1 = "INSERT INTO tk_dlchamcong(ID,ma_chamcong, ma_nv, tg_check)values(md5('" + r["CardID"] + r["StaffID"] + r["TransDT"].ToString() + "'),'" + r["CardID"] + "', '" + r["StaffID"] + "', '" + r["TransDT"] + "')";
MYSQLDB.query(sql1);
}
}
catch {
}
}
return true;
}
catch { }
return false;
}我不能使用Date()属性,它只接受with Date。但是当我调试它的时候,它会像这样显示并跳到捕获错误。
无法比较,r["TransDT"]为DateTime。这是图像显示错误。

已更新: r"TransDT“是数据库中具有值的对象{string}:11/11/2015 18:03:11
我将其格式化为如下查询:FORMAT(TransDT,'dd/MM/yyyy HH:mm:ss') as TransDT from Transact
调试时出错:
发布于 2015-11-11 18:50:36
值可以是null(DBNull.Value),您可以尝试使用as-operator将其转换为DateTime?。当强制转换失败时,您会得到一个带有HasValue=false的Nullable<DateTime>
DateTime? TransDT = r["TransDT"] as DateTime?;
if(TransDT.HasValue && TransDT.Value.Date == DateTime.Today)
{
// ...
}你不需要while,一个if就足够了,因为你只有一个DataRow。
https://stackoverflow.com/questions/33648851
复制相似问题