You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When providing a custom TypeHandler for the type string, the IDbDataParameter.Size property is overwritten after calling the custom TypeHandler.SetValue method. The database provider I'm using does not accept nvarchar parameters with such a large size. The size property is being overridden by the block of code starting here:
I am unable to create a pull request, but my suggested fix is to move that conditional block prior to calling the custom handler just above. Or, if necessary to set the size after setting the value, add a check for a valid size before overriding it with the DbString.DefaultSize constant loaded earlier in the function.
Here is an example type handler and setup code to reproduce the issue:
// Custom string type handlerprivateclassStringTypeHandler:SqlMapper.TypeHandler<string>{publicoverridevoidSetValue(IDbDataParameterparameter,string?value){intsize=Math.Max(1,value?.Length??100);parameter.DbType=DbType.String;parameter.Value=value;parameter.Size=size;}publicoverridestring?Parse(objectvalue){returnvalueasstring;}}// Replacing the default string type handlerSqlMapper.RemoveTypeMap(typeof(string));SqlMapper.AddTypeHandler(newStringTypeHandler());IDbConnectionconn;conn.Execute(cmd,new{p="hello"})
I ran into this issue using Dapper version 2.1.35. My project is .NET 8 targeting win-x86 with an OdbcConnection using driver Microsoft Access Driver (*.mdb). Trying to execute a statement resulted in this error: System.Data.Odbc.OdbcException (0x80131937): ERROR [HY104] [Microsoft][ODBC Microsoft Access Driver]Invalid precision value. I can workaround this error by changing the type passed for parameters to use DbString. However, I would like to reuse a datatype that has string properties so I created a custom TypeHandler<string>. Through debugging, I found that the parameter size was set to 4000 when executing the command, despite my handler setting the size just moment earlier.
The text was updated successfully, but these errors were encountered:
When providing a custom
TypeHandler
for the typestring
, theIDbDataParameter.Size
property is overwritten after calling the customTypeHandler.SetValue
method. The database provider I'm using does not accept nvarchar parameters with such a large size. The size property is being overridden by the block of code starting here:Dapper/Dapper/SqlMapper.cs
Line 2844 in 6434c69
I am unable to create a pull request, but my suggested fix is to move that conditional block prior to calling the custom handler just above. Or, if necessary to set the size after setting the value, add a check for a valid size before overriding it with the
DbString.DefaultSize
constant loaded earlier in the function.Here is an example type handler and setup code to reproduce the issue:
I ran into this issue using Dapper version 2.1.35. My project is .NET 8 targeting
win-x86
with anOdbcConnection
using driverMicrosoft Access Driver (*.mdb)
. Trying to execute a statement resulted in this error:System.Data.Odbc.OdbcException (0x80131937): ERROR [HY104] [Microsoft][ODBC Microsoft Access Driver]Invalid precision value
. I can workaround this error by changing the type passed for parameters to useDbString
. However, I would like to reuse a datatype that has string properties so I created a customTypeHandler<string>
. Through debugging, I found that the parameter size was set to 4000 when executing the command, despite my handler setting the size just moment earlier.The text was updated successfully, but these errors were encountered: