DevExpress DateEdit组件是空日期,传入SQL参数报错解决方法
DevExpress DateEdit组件是空日期,传入SQL参数报错解决方法
ConvertEx.cs增加一个通用方法,将所有日期类型转换为SQL支持的日期格式。
SQL日期范围:1753-01-01 ~ 9999-12-31
调用方式:
扫一扫加作者微信
ConvertEx.cs增加一个通用方法,将所有日期类型转换为SQL支持的日期格式。
SQL日期范围:1753-01-01 ~ 9999-12-31
C# Code:
/// <summary>
/// 转换为SQL支持的日期格式, 日期范围:1753-1-1 ~ 9999-12-31
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static DateTime ToSqlDateTime(object o)
{
DateTime defMinValue = DateTime.Parse("1753-01-01 00:00:00");
DateTime defMaxValue = DateTime.Parse("9999-12-31 23:59:59");
if (null == o) return defMinValue;//传入空值,返回预设值
DateTime dt;
if (DateTime.TryParse(o.ToString(), out dt))
{
if (dt < defMinValue || dt > defMaxValue)
return defMinValue;//无效日期,预设返回SQL支持的最小日期
else
return dt;
}
return defMinValue;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// 转换为SQL支持的日期格式, 日期范围:1753-1-1 ~ 9999-12-31
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static DateTime ToSqlDateTime(object o)
{
DateTime defMinValue = DateTime.Parse("1753-01-01 00:00:00");
DateTime defMaxValue = DateTime.Parse("9999-12-31 23:59:59");
if (null == o) return defMinValue;//传入空值,返回预设值
DateTime dt;
if (DateTime.TryParse(o.ToString(), out dt))
{
if (dt < defMinValue || dt > defMaxValue)
return defMinValue;//无效日期,预设返回SQL支持的最小日期
else
return dt;
}
return defMinValue;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
调用方式:
C# Code:
public DataSet GetReportDemo(string docNoFrom, string docNoTo, DateTime docDateFrom, DateTime docDateTo)
{
SqlProcedure sp = SqlBuilder.BuildSqlProcedure("usp_IN_GetReport");
sp.AddParam("@DocNoFrom", SqlDbType.VarChar, docNoFrom);
sp.AddParam("@DocNoTo", SqlDbType.VarChar, docNoTo);
sp.AddParam("@DocDateFrom", SqlDbType.DateTime,ConvertEx.ToSqlDateTime(docDateFrom));
sp.AddParam("@DocDateTo", SqlDbType.DateTime, ConvertEx.ToSqlDateTime(docDateTo));
return DataProvider.Instance.GetDataSet(_DBName, sp.SqlCommand);
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
public DataSet GetReportDemo(string docNoFrom, string docNoTo, DateTime docDateFrom, DateTime docDateTo)
{
SqlProcedure sp = SqlBuilder.BuildSqlProcedure("usp_IN_GetReport");
sp.AddParam("@DocNoFrom", SqlDbType.VarChar, docNoFrom);
sp.AddParam("@DocNoTo", SqlDbType.VarChar, docNoTo);
sp.AddParam("@DocDateFrom", SqlDbType.DateTime,ConvertEx.ToSqlDateTime(docDateFrom));
sp.AddParam("@DocDateTo", SqlDbType.DateTime, ConvertEx.ToSqlDateTime(docDateTo));
return DataProvider.Instance.GetDataSet(_DBName, sp.SqlCommand);
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网