C# Datetime to ODBC Datetime conversion error(C#日期时间到ODBC日期时间转换错误)
问题描述
我们正在尝试将ODBC与SQL Server 2012一起用于我们的新应用程序,因为MS正在逐步淘汰OleDb,我们希望让它变得更简单(更容易?)以移植到其他数据库。
问题是,当我尝试使用ODBC数据访问类时,在尝试保存DateTime时收到以下错误:
错误[22008][微软][SQL Server Native Client 11.0]日期时间字段 溢出来了。中指定的小数位数秒精度 参数绑定。
好的,我明白这一点。C#具有比ODBC参数要使用的更高的精度级别。我知道ODBC实现不喜欢秒的分数,但是有什么办法来保持日期时间的精确度呢?
谢谢,
丁斯代尔
编辑:好的,这里是原始参数构造函数:
String Name = "created";
DateTime DateTimeValue = DateTime.Now;
OdbcCommand cmd = new OdbcCommand();
cmd.CommandType = request.CommandType;
cmd.CommandText = request.Command;
OdbcParameter param;
param = new OdbcParameter(Name, OdbcType.DateTime);
param.Value = DateTimeValue;
cmd.Parameters.Add(param);
此代码生成以下错误:
错误[22008][MICROSOFT][SQL Server Native Client 11.0]日期时间字段溢出。小数秒精度超过参数绑定中指定的小数位数。
修复方法是使用扩展构造函数,如下所示:
param = new OdbcParameter(Name, OdbcType.DateTime, 23, System.Data.ParameterDirection.Input, false, 0, 3, Name, DataRowVersion.Current, DateTimeValue);
再次感谢库莫德! 丁斯代尔
推荐答案
小数秒精度超过参数绑定中指定的小数位数。
表示第二个精度大于OdbcParameter
对象的Scale
属性。
对于DateTime
格式:
yyyy-mm-dd hh:mm
-Precision = 16, Scale = 0
yyyy-mm-dd hh:mm:ss
-Precision = 19, Scale = 0
这里是需要超过Scale
属性的(默认为0
)
yyyy-mm-dd hh:mm:ss.f
-Precision = 21, Scale = 1
yyyy-mm-dd hh:mm:ss.ff
-Precision = 22, Scale = 2
yyyy-mm-dd hh:mm:ss.fff
-Precision = 23, Scale = 3
每个f
都是额外的小数位秒精度
OdbcParameter param = new OdbcParameter( "name", OdbcType.DateTime );
param.Value = DateTime.Now;
param.Precision = 23;
param.Scale = 3; // <-- you need to set this property
这篇关于C#日期时间到ODBC日期时间转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C#日期时间到ODBC日期时间转换错误


基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01