将连接字符串中的 Provider 更改为 TLS1.2 Compatible 会导致参数因 datetime 而失败

Changing Provider in connection string to be TLS1.2 Compatible causes parameters to fail with datetime

我有一个使用 oledb 的现有应用程序,现在需要升级以兼容 TLS1.2。因此,我为 SQL Server® 下载了 Microsoft® OLE DB Driver 18,为了让它工作,我需要更改

连接字符串中的提供程序
Provider=SQLOLEDB; Server={0}; Database={1}; {2}; Connection Timeout=60;

Provider=SQLNCLI11; Server={0}; Database={1}; {2}; Connection Timeout=60;

这导致我之前的查询添加了这样的参数:

cmd.Parameters.AddWithValue("@lastRun", lastRun.ToUniversalTime)

我试图查询参数的列具有日期时间类型

当我使用这些参数执行查询时,它们都失败并显示如下错误消息:

"Conversion failed for command parameter[0] '' because the data value overflowed the type used by the provider."

内部异常为:

"The fractional part of the provided time value overflows the scale of the corresponding SQL Server parameter or column. Increase bScale in DBPARAMBINDINFO or column scale to correct this error."

这是堆栈跟踪: 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult 小时) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,对象& executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(对象和执行结果) 在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为,字符串方法) 在 System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior 行为) 在 System.Data.Common.DbDataAdapter.FillInternal(DataSet 数据集、DataTable[] 数据表、Int32 startRecord、Int32 maxRecords、String srcTable、IDbCommand 命令、CommandBehavior 行为) 在 System.Data.Common.DbDataAdapter.Fill(DataTable[] 数据表、Int32 startRecord、Int32 maxRecords、IDbCommand 命令、CommandBehavior 行为) 在 System.Data.Common.DbDataAdapter.Fill(DataTable 数据表)

如果我将提供程序改回 SQLOLEDB,那么它将正常工作。 到目前为止,我只发现了日期字段的问题。我还没有做广泛的测试来验证其他领域也可能有这个问题。 有谁知道是什么导致了这种情况以及可能的解决方案,而无需更改之前编写的数万个查询?

我遇到了同样的问题。似乎有 2 个解决方案,都在更改代码

(1) 自己管理毫秒而不是使用 DateTime.Now。以下代码将获取没有毫秒的 Now 日期

DateTime currentDate = System.DateTime.Now;
currentDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, currentDate.Hour, currentDate.Minute, currentDate.Second);  // removes milliseconds & ticks

(2) 设置OleDbParameter的比例

OleDbParameter parameter = new OleDbParameter("Price", OleDbType.Decimal); 
parameter.Value = 3.1416; 
parameter.Precision = 8; 
parameter.Scale = 4;