简体   繁体   English

操作数类型冲突:varchar 与尝试插入加密数据库的 varchar(50) 不兼容

[英]Operand type clash: varchar is incompatible with varchar(50) trying to insert in encrypted database

I am getting a SqlException :我收到一个SqlException

Operand type clash: varchar is incompatible with varchar(50) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_database_name = 'PB') collation_name = 'SQL_Latin1_General_CP1_CI_AS'\\r\\nIncorrect parameter encryption metadata was received from the client.操作数类型冲突:varchar 与使用加密的 varchar(50) 不兼容 (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_name_1L_General encryption_property_key_database_1Latinr_Inc从客户端收到元数据。 The error occurred during the invocation of the batch and therefore the client can refresh the parameter encryption metadata by calling sp_describe_parameter_encryption and retry.该错误发生在批处理调用期间,因此客户端可以通过调用 sp_describe_parameter_encryption 刷新参数加密元数据并重试。

My C# code:我的 C# 代码:

using (var connection = new SqlConnection(GetConnectionString()))
{
    using (var cmd = new SqlCommand("Clients_Insert", connection))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 50).Value = client.Email;
        cmd.Parameters.Add("@ContactPerson", SqlDbType.VarChar, 400).Value = client.ContactPerson;

        connection.Open();
        cmd.ExecuteNonQuery();
    }
}

And my stored procedure:还有我的存储过程:

ALTER PROCEDURE [dbo].[Clients_Insert]
    @Email VARCHAR(50),
    @ContactPerson VARCHAR(400)
AS
BEGIN
    INSERT into dbo.Clients(Email, ContactPerson) 
    VALUES (@Email, @ContactPerson);

    SELECT SCOPE_IDENTITY();
END;

I have no problems with inserting data into not encrypted fields.我将数据插入未加密的字段没有问题。

I found this article我找到了这篇文章

http://dataap.org/sql-2016-ctp/column-level-encryption-using-always-encrypted-in-sql-server-2016/ http://dataap.org/sql-2016-ctp/column-level-encryption-using-always-encrypted-in-sql-server-2016/

My issue is similiar but i haven't found the solution.我的问题是类似的,但我还没有找到解决方案。

There are 2 things you can try,有2件事你可以尝试,

Ensure that Column encryption setting is enabled in your connection string.确保在您的连接字符串中启用了列加密设置。 This can be done using a SqlConnectionStringBuilder object and setting SqlConnectionStringBuilder.ColumnEncryptionSetting to Enabled as follows这可以使用SqlConnectionStringBuilder对象并将SqlConnectionStringBuilder.ColumnEncryptionSetting设置为Enabled ,如下所示

strbldr.ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled;

If your stored procedure was created before you encrypted your column, you will need to refresh metadata for your stored procedure as follows如果您的存储过程是在加密列之前创建的,则需要按如下方式刷新存储过程的元数据

Use [Database]
GO    
--Do this for all stored procedures
EXEC sys.sp_refresh_parameter_encryption @name = '[dbo].[Clients_Insert]'

If someone is still looking for an answer on this, what worked for me is that you need to use DbType.AnsiStringFixedLength data type in the SqlParameter data type, for the encrypted columns.如果有人仍在为此寻找答案,那么对我DbType.AnsiStringFixedLength是您需要在 SqlParameter 数据类型中使用DbType.AnsiStringFixedLength数据类型,用于加密列。

Refer this article for more information 请参阅这篇文章以获取更多信息

任何仍然面临这个问题并且上述检查都没有帮助的人,请确保过程和代码中的参数名称完全匹配(区分大小写)。

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

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