简体   繁体   English

无法将 C# 的默认日期时间插入到 SQL Server 2008

[英]Unable to insert default DateTime of C# to SQL Server 2008

I'm working on a C# console application project.我正在处理 C# 控制台应用程序项目。 Trying to insert a DateTime value into SQL Server 2008 and this is working good.尝试将DateTime值插入到 SQL Server 2008 中,效果很好。 In case if my code fails I want to insert the default DateTime to the table in database.如果我的代码失败,我想将默认的DateTime插入数据库中的表中。

When I tried to insert I got an error.当我尝试插入时出现错误。

SqlTypeException was unhandled: SqlTypeException 未处理:
SqlDateTime overflow. SqlDateTime 溢出。 Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.必须在 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。

Below I have pasted the specific code.下面我粘贴了具体的代码。

try
{
  ...
  return DateTime.Now;
}

catch (WebException ex)
{
  ...
  return default(DateTime);
}

Code I used to insert details in the LOGS table:我用来在 LOGS 表中插入详细信息的代码:

connection.Open();
for (int i = 0; i < LogList.Count; i++)
    {
        string aprocessLogQuery = "INSERT INTO PROCESS_LOGS VALUES (@fileId, @startTime, @endTime, @transferredTime)";
        command = new SqlCommand(aprocessLogQuery, connection);
        command.Parameters.Add("fileId", SqlDbType.Int).Value = LogList[i].fileId;
        command.Parameters.Add("startTime", SqlDbType.DateTime).Value = LogList[i].fileGeneration_StartDateTime;
        command.Parameters.Add("endTime", SqlDbType.DateTime).Value = LogList[i].fileGeneration_EndDateTime;
        command.Parameters.Add("transferredTime", SqlDbType.DateTime).Value = LogList[i].fileTransferred_DateTime;
        dataReader.Close();
        dataReader = command.ExecuteReader();
    }
connection.Close();

The default value of DateTime is 01/01/0001 12:00:00 AM , therefore it is not working. DateTime的默认值为01/01/0001 12:00:00 AM ,因此它不起作用。 Can anyone please suggest alternate working solution?任何人都可以建议替代工作解决方案吗?

Thanks谢谢

Another alternative solution is you can work around by build extension method look like as below:另一种替代解决方案是您可以通过构建扩展方法来解决,如下所示:

public static class DateTimeExtension
{
    public static DateTime DefaultSqlDateTime(this DateTime dateTime)
    {
       return new DateTime(1753, 1, 1, 12, 0, 0);
    }
}

and instead of return default(DateTime) you can use variable dateTime return dateTime.DefaultSqlDateTime;而不是return default(DateTime)你可以使用变量 dateTime return dateTime.DefaultSqlDateTime;

EDIT: How to use extension method:编辑:如何使用扩展方法:

Eg: return default(DateTime).GetDefaultSqlDateTime();例如: return default(DateTime).GetDefaultSqlDateTime();

or要么

DateTime dateTime = ...
return dateTime.GetDefaultSqlDateTime();

In the database, an unknown date is represented with NULL value.在数据库中,未知日期用NULL值表示。 You can insert that like:您可以像这样插入:

command.Parameters.AddWithValue("@startTime", DBNull.Value);

(As far as I know, it's required to prefix your parameters with @ .) (据我所知,需要在参数前加上@前缀。)

I am using this function in my Utilities instead of regular TryParse我在我的实用程序中使用这个函数而不是常规的 TryParse

public static bool TryParseSqlDateTime(string someval, DateTimeFormatInfo dateTimeFormats, out DateTime tryDate)
    {
        bool valid = false;
        tryDate = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
        System.Data.SqlTypes.SqlDateTime sdt;
        if (DateTime.TryParse(someval, dateTimeFormats, DateTimeStyles.None, out tryDate))
        {
            try
            {
                sdt = new System.Data.SqlTypes.SqlDateTime(tryDate);
                valid = true;
            }
            catch (System.Data.SqlTypes.SqlTypeException ex)
            {


            }
        }

        ret

You can use this, this will return minimum date value that SQL will accept.您可以使用它,这将返回 SQL 将接受的最小日期值。 This method return DateTime in .Net.此方法在 .Net 中返回 DateTime。

System.Data.SqlTypes.SqlDateTime.MinValue.Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM