In SSMS we are attempting to bulk insert from a csv file into a table that has a column encrypted using SQL Server 2016's Always Encrypted feature.
This is the command we're using:
INSERT INTO membersE
SELECT *
FROM OPENROWSET(
BULK 'c:\members.csv',
FORMATFILE = 'c:\membersEFormat.xml',
FIRSTROW = 2
) m
This returns the typical error you get when attempting to insert into an encrypted column:
Msg 206, Level 16, State 2, Line 6
Operand type clash: varbinary 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 = 'DATABASE') collation_name = 'Latin1_General_BIN2'
We understand that you can't insert into an encrypted column via SSMS and that you need to use a .NET 4.6.1+ client, but we'd like to know if bulk insert operations are not possible as well?
SAMPLE CODE THAT WORKED FOR ME
(to satisfy Windows10's request)
SqlCommand cmd;
SqlConnection conn;
SqlBulkCopy copy;
SqlDataAdapter da;
DataTable dt;
using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
{
conn.Open();
using (cmd = new SqlCommand("SELECT * FROM members", conn))
using (copy = new SqlBulkCopy(conn))
using (da = new SqlDataAdapter(cmd))
using (dt = new DataTable())
{
cmd.CommandTimeout = 600;
da.Fill(dt);
cmd.CommandText = "TRUNCATE TABLE membersE";
cmd.ExecuteNonQuery();
copy.DestinationTableName = "membersE";
copy.WriteToServer(dt);
}
}