I am using C# to interface with a SQL database. the database has a DateTime field. When I try to write a DateTime object from C#, I get the following error:
ERROR [22008] [Microsoft][ODBC SQL Server Driver]Datetime field overflow
I found this on the topic:
Is there any manipulation I can do to my DateTime object on the C# side?
EDITS I am trying to add DateTime.MinValue
Sounds like you are just passing in a bad date. For example, DateTime.MinValue is outside the accepted range for SQL Server datetime values by about 1600 years.
By the way, you shouldn't be using ODBC for C# to SQL Server communication. Using System.Data.SqlClient will give you much better performance.
The .NET DateTime object has a bigger range than the SQL date/time field.
In your data access layer, anytime your writing a date to the database, ensure that is inside the rate of SqlDateTime.MinValue and SqlDateTime.MaxValue.
我很安静,您的.NET DateTime未初始化,这意味着它是“0000-01-01”,这不是SQL Server DATETIME的有效值, 通常不是所需的值;-)
Change all your datetime columns to datetime2 type. Generate the sql scripts using the query below.
select distinct concat('alter table ', table_name, ' alter column ', column_name, ' datetime2 ',
case when(is_nullable = 'NO') then ' NOT ' else '' end, ' NULL;')
from information_schema.columns where data_type = 'datetime';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.