简体   繁体   中英

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. 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

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."

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."

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)

if I change the provider back to SQLOLEDB then it would work fine. 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

(1) Manage the milliseconds yourself instead of using DateTime.Now. The following code will get the Now date without milliseconds

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

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

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.

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