简体   繁体   English

将连接字符串中的Provider更改为TLS1.2兼容会导致参数在datetime时失败

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

I have an existing application that is using oledb through out the application now is required to upgrade to be TLS1.2 compatible. 我有一个使用oledb的现有应用程序,现在需要升级到TLS1.2兼容的应用程序。 So I downloaded the Microsoft® OLE DB Driver 18 for SQL Server® , to get it to work I needed to change my provider in the connection string from 因此,我下载了用于SQLServer®的Microsoft®OLE DB Driver 18,以使其正常工作,我需要在

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

to

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

This caused my previous queries with parameters added like this: 这导致我先前的查询中添加了如下参数:

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

the column that I am trying to query the param with has the type of datetime 我尝试查询参数的列具有日期时间类型

when I execute queries with parameters such as these they all fail with error message like this: 当我使用此类参数执行查询时,它们都将失败,并显示以下错误消息:

"Conversion failed for command parameter[0] '' because the data value overflowed the type used by the provider." “命令参数[0]的转换失败,因为数据值溢出了提供程序使用的类型。”

with inner exception of: 内部例外:

"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." “提供的时间值的小数部分会溢出相应的SQL Server参数或列的小数位数。增加DBPARAMBINDINFO或列小数位数的bScale可以纠正此错误。”

and here is the stack trace: at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) 这是堆栈跟踪:在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object&executeResult)在System.Data.OleDb.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)在System.Data.OleDb.OleDbCommand。 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior行为,String方法)的System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior行为)的System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,DataTable []的executeResult)数据表,位于System.Data.Common.DbDataAdapter.Fill处的Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)(位于System.Data处的DataTable [] dataTables,Int32 startRecord,Int32 maxRecords,IDbCommand命令,CommandBehavior行为)。 Common.DbDataAdapter.Fill(数据表dataTable)

if I change the provider back to SQLOLEDB then it would work fine. 如果我将提供程序改回SQLOLEDB,那么它将正常工作。 So far I have only discovered issue with date field. 到目前为止,我只发现日期字段存在问题。 I haven't done extensive testing to verify other fields may have this issue too. 我尚未进行广泛的测试以验证其他领域也可能存在此问题。 Does anyone know what is causing this and possible solutions without changing tens of thousands of queries that was previously written? 没有人知道是什么原因导致了这种情况以及可能的解决方案,而没有更改以前编写的成千上万的查询吗?

I have encountered the same issue. 我遇到了同样的问题。 There seems to be 2 solutions, both are changing code 似乎有2个解决方案,两者都在更改代码

(1) Manage the milliseconds yourself instead of using DateTime.Now. (1)自己管理毫秒,而不要使用DateTime.Now。 The following code will get the Now date without milliseconds 以下代码将获取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) Set the scale of the OleDbParameter (2)设置OleDbParameter的小数位数

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

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

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