如何在加密列中插入 NULL/empty 字符串 - Always Encrypted
How to insert NULL/empty string in encrypted column - Always Encrypted
我已经使用 SQL Server 2016 always encrypt 方法加密了现有数据,我们拥有的列之一是 NULLABLE,但是从屏幕插入时它不接受 NULL 或空字符串。如果加密列在屏幕上不是必需的,如何让它工作?
您看到的错误是不正确的,请确保您正确传递了参数。以下是有关如何执行此操作的一些示例代码。
架构:
CREATE TABLE [dbo].[SO](
[ssn] [nvarchar](9) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL
)
GO
CREATE PROCEDURE dbo.insertSP @ssn nvarchar(9)
AS
INSERT INTO [dbo].[SO] ([SSN]) VALUES (@SSN);
GO
C#代码:
SqlConnectionStringBuilder strbldr = new SqlConnectionStringBuilder();
strbldr.DataSource = ".";
strbldr.InitialCatalog = @"exptdb";
strbldr.IntegratedSecurity = true;
strbldr.ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled;
string ssn = "";
using (var conn = new SqlConnection(strbldr.ConnectionString))
using (var command = conn.CreateCommand()) {
command.CommandType = CommandType.StoredProcedure;
command.CommandText = @"dbo.insertSP";
SqlParameter paramSSN = command.CreateParameter();
paramSSN.ParameterName = "@ssn";
paramSSN.SqlDbType = SqlDbType.NVarChar;
paramSSN.Direction = ParameterDirection.Input;
paramSSN.Value = ssn;
paramSSN.Size = 9;
command.Parameters.Add(paramSSN);
conn.Open();
command.ExecuteNonQuery();
}
注意,即在上述场景中,如果
string ssn = "";
则查询成功,但是如果
string ssn = null;
你应该看到执行失败
Additional information: Procedure or function 'insertSP' expects
parameter '@ssn', which was not supplied.
即使 ssn 列为明文也会出现此故障
您可以按如下方式在加密列中插入空值,因为空值未加密:
using (var conn = new SqlConnection(strbldr.ConnectionString))
using (var command = conn.CreateCommand()) {
command.CommandText = @"INSERT INTO [dbo].[SO] ([SSN]) VALUES (null)";
conn.Open();
command.ExecuteNonQuery();
}
我已经使用 SQL Server 2016 always encrypt 方法加密了现有数据,我们拥有的列之一是 NULLABLE,但是从屏幕插入时它不接受 NULL 或空字符串。如果加密列在屏幕上不是必需的,如何让它工作?
您看到的错误是不正确的,请确保您正确传递了参数。以下是有关如何执行此操作的一些示例代码。
架构:
CREATE TABLE [dbo].[SO](
[ssn] [nvarchar](9) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [CEK_Auto1], ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL
)
GO
CREATE PROCEDURE dbo.insertSP @ssn nvarchar(9)
AS
INSERT INTO [dbo].[SO] ([SSN]) VALUES (@SSN);
GO
C#代码:
SqlConnectionStringBuilder strbldr = new SqlConnectionStringBuilder();
strbldr.DataSource = ".";
strbldr.InitialCatalog = @"exptdb";
strbldr.IntegratedSecurity = true;
strbldr.ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled;
string ssn = "";
using (var conn = new SqlConnection(strbldr.ConnectionString))
using (var command = conn.CreateCommand()) {
command.CommandType = CommandType.StoredProcedure;
command.CommandText = @"dbo.insertSP";
SqlParameter paramSSN = command.CreateParameter();
paramSSN.ParameterName = "@ssn";
paramSSN.SqlDbType = SqlDbType.NVarChar;
paramSSN.Direction = ParameterDirection.Input;
paramSSN.Value = ssn;
paramSSN.Size = 9;
command.Parameters.Add(paramSSN);
conn.Open();
command.ExecuteNonQuery();
}
注意,即在上述场景中,如果
string ssn = "";
则查询成功,但是如果
string ssn = null;
你应该看到执行失败
Additional information: Procedure or function 'insertSP' expects parameter '@ssn', which was not supplied.
即使 ssn 列为明文也会出现此故障
您可以按如下方式在加密列中插入空值,因为空值未加密:
using (var conn = new SqlConnection(strbldr.ConnectionString))
using (var command = conn.CreateCommand()) {
command.CommandText = @"INSERT INTO [dbo].[SO] ([SSN]) VALUES (null)";
conn.Open();
command.ExecuteNonQuery();
}