【问题标题】:How to insert date into sql server through dateTimePicker in windows forms?如何在 windows 窗体中通过 dateTimePicker 将日期插入 sql server?
【发布时间】:2015-02-22 15:51:13
【问题描述】:

我在 Windows 窗体应用程序中有一个包含识别日期字段的表单。表单应该在其他字段中将此日期插入到 sql server db 中的表中

应该存储日期的列是日期时间类型。

我的问题是尝试执行插入操作时收到异常。

日期以这种格式存储在数据库中:2013-01-01 00:00:00.000

我尝试将日期选择器的格式更改为 yyyy-MM-dd hh:mm:ss 以匹配数据库中数据的格式,但我仍然遇到该异常。我什至尝试使用文本框而不是日期选择器,但我无法摆脱异常。

我的应用程序正在使用 Entity Framework 6 连接到数据库。

更新 1:

这是模型类:

public partial class Material
{
    public Material()
    {
        this.ItemMaterials = new HashSet<ItemMaterial>();
    }

    public int Code { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Unit { get; set; }
    public Nullable<decimal> UnitPrice { get; set; }
    public System.DateTime IdentDate { get; set; }

    public virtual ICollection<ItemMaterial> ItemMaterials { get; set; }
}

我还依赖 here 解释的控件来处理 CRUD 操作。

异常给了我这个信息:

{"将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围。\r\n语句已终止。"}

堆栈跟踪:

在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用者HasConnectionLock,布尔异步关闭) 在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean & dataReady) 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串 resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,布尔异步,Int32 超时,任务和任务,布尔异步写入,SqlDataReader ds) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,String 方法,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 完成,String methodName,布尔 sendToPipe,Int32 超时,布尔 asyncWrite) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TInterceptionContext,TResult](Func1 操作,TInterceptionContext 拦截上下文,Action1 executing, Action1 执行) 在 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand 命令,DbCommandInterceptionContext 拦截上下文) 在 System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() 在 System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary2 identifierValues, List1 generatedValues) 在 System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()

【问题讨论】:

  • 你没有展示任何代码,这让人很难知道它有什么问题......
  • 使用日期选择器的 DateTime 值,而不是格式化的值。
  • @JonSkeet 事实上,没有太多代码可以展示,因为保存和添加是通过这个控件来处理的,该控件是为了处理表单和 EF codeproject.com/Articles/221931/Entity-Framework-in-WinForms 之间的绑定而创建的添加控件后dropbox.com/s/rcxkspg9fiyqhqw/form.png?dl=0
  • 好吧,你至少可以向我们展示模型类...
  • 格式应该完全不相关。您的模型使用日期时间,而不是字符串 - 根本不应该涉及文本表示。

标签: c# sql-server winforms entity-framework datetime


【解决方案1】:

DateTime 属性设置为 DateTime.MinValue (1/1/0001) 时,通常会发生该异常。请务必调试并检查以确保任何和所有DateTime 属性(甚至可以为空的)都未设置为DateTime.MinValue。请务必检查您的 Material 对象以及任何子对象。

为什么它抛出这个异常是因为 EF 将 C# 的 DateTime 映射到 SQL 的 datetime 类型,这通常是可以的,除了 C# 的 DateTime 可以使用 datetime 不能使用的日期(但 SQL 的 @987654332 @ 能够)。当您尝试使用 SQL 的 datetime 不支持的值保存 DateTime 时,它认为您正在尝试将 datetime2 值保存到当前指定为 datetime 的列中。

文档:

datetime

datetime2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多