繁体   English   中英

如何通过Windows窗体中的dateTimePicker将日期插入SQL Server?

[英]How to insert date into sql server through dateTimePicker in windows forms?

我在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; }
}

我还依赖于此处说明的控件来处理CRUD操作。

异常给了我这个信息:

{“ datetime2数据类型到datetime数据类型的转换导致值超出范围。\\ r \\ n该语句已终止。”}

堆栈跟踪 :

在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔型breakConnection,动作1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)在System.Data.SqlClient.TdsParser .System.Data.SqlClient.TdsParser.TryRun(ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)at Run。在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean异步,Int32超时,Task&task,Boolean异步数据在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds()上运行的FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)) .SqlClient.SqlCommand.RunExecuteReader(C ommandBehavior cmdBehavior,RunBehavior runBehavior,布尔返回流,字符串方法,TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1完成,字符串methodName,布尔型sendToPipe,Int32 System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch[TInterceptionContext,TResult](Func ()上的1.Dispatch[TInterceptionContext,TResult](Func1.Dispatch[TInterceptionContext,TResult](Func 1操作,TInterceptionContext拦截上下文, 1 executing, Action 1,执行1 executing, Action 1)在System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()处System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()处System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery() System.Data.Entity.Core.Mapping.Update上的DynamicUpdateCommand.Execute(字典2 identifierValues, List 1个GeneratedValues) .Internal.UpdateTranslator.Update()

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

之所以抛出该异常,是因为EF将C#的DateTime映射到SQL的datetime类型,通常可以,但C#的DateTime可以使用datetime不能的datetime2 (但是SQL的datetime2可以)。 当您尝试使用SQL的datetime不支持的值保存DateTime ,它认为您正在尝试将datetime2值保存到当前指定为datetime的列中。

文档:

约会时间

DATETIME2

暂无
暂无

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

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