Dapper C# 的可转换问题
Iconvertible Issue with dapper C#
需要帮助!
我正在尝试使用 Dapper 从数据库中获取一个值,但是当我执行查询<> 指令时,我收到了一个 "object must implement Iconvertible" 异常。我做错了什么以及如何解决?
Decrypt方法调用ExceuteQuery函数时出现错误
代码
计划
ServiceSettingsEntity appSetting = MainRepository.GetConfigSettings(appSettingKey.ToString(), companyCode);
if (appSetting.IsEncrypted)
appSetting.Value = MainRepository.Decrypt(appSetting.Value);
return appSetting.Value.Trim();
解密函数
public static string Decrypt(string encryptedData)
{
CommandSettings commandSettings = new CommandSettings
{
CommandText = @"[Utility].[DecryptData]",
CommandType = CommandType.StoredProcedure,
Parameters = new
{
@DataToDecrypt = encryptedData
}};
return new MsSqlProviderBase(EdxDbConnectionString,
commandSettings).ExecuteQuery<string>().FirstOrDefault();
}
用于封装dapper Query<>函数的ExecuteQuery函数
public List<T> ExecuteQuery<T>()
{
using (IDbConnection dbConnection = DbConnection)
{
List<T> qResult = dbConnection.Query<T>(CommandSettings.CommandText,
CommandSettings.Parameters,
commandTimeout: CommandSettings.CommandTimeout,
commandType:
CommandSettings.CommandType).ToList();
return qResult;
}
}
(基于评论中的扩展信息。)大多数 IDbConnection
实现将(正确地)"translate" SQL varbinary
到 C# byte[]
。 byte[]
和 string
不能立即转换,因为 natural language text is complex.
在底层存储过程可用之前,您需要使用编码将 string
转换为 byte[]
,反之亦然。
参见 Microsoft Docs。结果:一旦你 select 编码,你将使用 GetBytes(string)
和 GetString(byte[])
方法来 en/decode 进入和离开存储过程的文本。
需要帮助!
我正在尝试使用 Dapper 从数据库中获取一个值,但是当我执行查询<> 指令时,我收到了一个 "object must implement Iconvertible" 异常。我做错了什么以及如何解决?
Decrypt方法调用ExceuteQuery函数时出现错误
代码
计划
ServiceSettingsEntity appSetting = MainRepository.GetConfigSettings(appSettingKey.ToString(), companyCode);
if (appSetting.IsEncrypted)
appSetting.Value = MainRepository.Decrypt(appSetting.Value);
return appSetting.Value.Trim();
解密函数
public static string Decrypt(string encryptedData)
{
CommandSettings commandSettings = new CommandSettings
{
CommandText = @"[Utility].[DecryptData]",
CommandType = CommandType.StoredProcedure,
Parameters = new
{
@DataToDecrypt = encryptedData
}};
return new MsSqlProviderBase(EdxDbConnectionString,
commandSettings).ExecuteQuery<string>().FirstOrDefault();
}
用于封装dapper Query<>函数的ExecuteQuery函数
public List<T> ExecuteQuery<T>()
{
using (IDbConnection dbConnection = DbConnection)
{
List<T> qResult = dbConnection.Query<T>(CommandSettings.CommandText,
CommandSettings.Parameters,
commandTimeout: CommandSettings.CommandTimeout,
commandType:
CommandSettings.CommandType).ToList();
return qResult;
}
}
(基于评论中的扩展信息。)大多数 IDbConnection
实现将(正确地)"translate" SQL varbinary
到 C# byte[]
。 byte[]
和 string
不能立即转换,因为 natural language text is complex.
在底层存储过程可用之前,您需要使用编码将 string
转换为 byte[]
,反之亦然。
参见 Microsoft Docs。结果:一旦你 select 编码,你将使用 GetBytes(string)
和 GetString(byte[])
方法来 en/decode 进入和离开存储过程的文本。