mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Support DateOnly TimeOnly
This commit is contained in:
parent
60363db476
commit
6afc6180e2
@ -162,6 +162,16 @@ namespace SqlSugar
|
||||
{
|
||||
this.DbType = System.Data.DbType.UInt16;
|
||||
}
|
||||
else if (type.Name == "TimeOnly")
|
||||
{
|
||||
this.DbType = System.Data.DbType.Time;
|
||||
this.Value = UtilMethods.TimeOnlyToTimeSpan(this.Value);
|
||||
}
|
||||
else if (type.Name == "DateOnly")
|
||||
{
|
||||
this.DbType = System.Data.DbType.Date;
|
||||
this.Value = UtilMethods.DateOnlyToDateTime(this.Value);
|
||||
}
|
||||
|
||||
}
|
||||
public SugarParameter(string name, object value, bool isOutput)
|
||||
|
@ -116,6 +116,19 @@ namespace SqlSugar
|
||||
if (destinationType.IsEnum && value is int)
|
||||
return Enum.ToObject(destinationType, (int)value);
|
||||
|
||||
if (destinationType.Name == "TimeOnly"&& sourceType.Name!= "TimeOnly")
|
||||
{
|
||||
var type = Type.GetType("System.TimeOnly", true, true);
|
||||
var method=type.GetMethods().FirstOrDefault(it => it.GetParameters().Length == 1 && it.Name == "FromTimeSpan");
|
||||
return method.Invoke(null, new object[] { value });
|
||||
}
|
||||
if (destinationType.Name == "DateOnly" && sourceType.Name != "DateOnly")
|
||||
{
|
||||
var type = Type.GetType("System.DateOnly", true, true);
|
||||
var method = type.GetMethods().FirstOrDefault(it => it.GetParameters().Length == 1 && it.Name == "FromDateTime");
|
||||
return method.Invoke(null, new object[] { value });
|
||||
}
|
||||
|
||||
if (!destinationType.IsInstanceOfType(value))
|
||||
return Convert.ChangeType(value, destinationType, culture);
|
||||
}
|
||||
@ -1064,5 +1077,19 @@ namespace SqlSugar
|
||||
{
|
||||
return $"[value=sql{UtilConstants.ReplaceKey}]";
|
||||
}
|
||||
|
||||
internal static object TimeOnlyToTimeSpan(object value)
|
||||
{
|
||||
if (value == null) return null;
|
||||
var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToTimeSpan");
|
||||
return method.Invoke(value, new object[] { });
|
||||
}
|
||||
|
||||
internal static object DateOnlyToDateTime(object value)
|
||||
{
|
||||
if (value == null) return null;
|
||||
var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToShortDateString");
|
||||
return method.Invoke(value, new object[] { });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user