ADO.NET将最小值到SQL Server 2008的日期列崩溃 [英] ADO.NET Insert Min Value into SQL Server 2008 Date column crashes

查看:115
本文介绍了ADO.NET将最小值到SQL Server 2008的日期列崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到以下几点:

using (var tx = sqlConnection.BeginTransaction())
{
 var command = sqlConnection.CreateCommand();
 command.Transaction = tx;
 command.CommandText = "INSERT INTO TryDate([MyDate]) VALUES(@p0)";
 var dateParam = command.CreateParameter();
 dateParam.ParameterName = "@p0";
 dateParam.DbType = DbType.Date;
 dateParam.Value = DateTime.MinValue.Date;
 command.Parameters.Add(dateParam);
 command.ExecuteNonQuery();
 tx.Commit();
}

,其中表中有一个SQL Server 2008的日期列。我可以通过SQL Management Studio中插入的'01 / 01/0001的值到这一点。

where the table has a SQL Server 2008 Date column. I can insert a value of '01/01/0001' into this via SQL Management Studio.

如果我运行上面的的ExecuteNonQuery我得到一个SQLDATETIME溢出。必须介于1753年1月1日上午12:00:00和12/31/9999下午11:59:59。例外。

If I run the above on the ExecuteNonQuery I get a "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM." exception.

这是为什么?在SQL Server日期字段确实接受01/01/0001。

Why is this? The SQL Server Date field does indeed accept 01/01/0001.

推荐答案

这个工程...

var command = sqlConnection.CreateCommand();
command.Transaction = tx;
command.CommandText = "INSERT INTO TryDate([MyDate]) VALUES(@p0)";
SqlParameter dateParam = new SqlParameter();
dateParam.ParameterName = "@p0";
dateParam.SqlDbType = SqlDbType.Date;
dateParam.Value = DateTime.MinValue.Date;
command.Parameters.Add(dateParam);
command.ExecuteNonQuery();

这篇关于ADO.NET将最小值到SQL Server 2008的日期列崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆