我能够读取和写入始终从 C# ASP.NET 应用程序加密的列。但是,我需要从 sqlcmd 执行此操作。

经过一些研究,我发现 here ,您需要 -g 参数来激活列加密设置,并更新到 sqlcmd 13.1 版,我已经使用“sqlcmd -?”进行了验证。

所以,我做了一个测试表:

create table tmp
(
tmp_id int identity(1,1) not null,
test varchar(500)
COLLATE Latin1_General_BIN2
ENCRYPTED WITH
   (ENCRYPTION_TYPE = RANDOMIZED,
    ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256',
    COLUMN_ENCRYPTION_KEY = MY_Encryption_Key)
null
);

并使用以下内容制作了一个 test.sql:
insert into tmp (test) values ('mytest');

我这样调用 sqlcmd:
sqlcmd.exe -g -b -S "localhost\SQL2016" -d "my_db_name" -i "D:\test.sql" -U "my_username" -P "my_password"

但是我收到以下错误,无论我是否使用“-g”:
Operand type clash: varchar is incompatible with varchar(8000) encrypted with (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'MY_Encryption_Key', column_encryption_key_database_name = 'my_db_name') collation_name = 'SQL_Latin1_General_CP1_CI_AS'

如果我将 test.sql 更改为:
declare @test varchar(8000) = 'mytest';
insert into tmp (test) values (@test);

然后我收到另一个错误(仍然有或没有 -g):
Encryption scheme mismatch for columns/variables '@test'. The encryption scheme for the columns/variables is (encryption_type = 'PLAINTEXT') and the expression near line '2' expects it to be (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'MY_Encryption_Key', column_encryption_key_database_name = 'my_db_name') (or weaker).

我需要这个,因为目前有一个 C# 应用程序接受一个平面文件,然后通过命令行调用“.sql”文件将它加载到数据库中。现在一些列需要加密。因此,我们需要 sqlcmd 来读/写加密列,以避免重写 C#.NET 中的所有逻辑。

难道我做错了什么?这甚至可能吗?为什么 sqlcmd 的“-g”参数没有影响?

最佳答案

目前, 仅在 SSMS 中支持使用以下语法将数据插入始终加密的列中。

declare @test varchar(8000) = 'mytest';
insert into tmp (test) values (@test);

您可以找到有关此 herehere 的详细信息

关于c# - SQL Server 2016 如何从命令行读取/写入始终加密的列?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44162849/

10-11 22:20
查看更多