OPEN MASTER KEY DECRYPTION BY PASSWORD = 'blabla' ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY CLOSE MASTER KEY OPEN SYMMETRIC KEY TestTableKey DECRYPTION BY CERTIFICATE EncryptTestCert declare @hexbin varbinary(max); set @hexbin = ENCRYPTBYKEY(KEY_GUID('TestTableKey'),N'1111'); CLOSE SYMMETRIC KEY TestTableKey select '0x' + cast('' as xml).value('xs:hexBinary(sql:variable("@hexbin") )', 'varchar(1024)') as Column1;
ConnectionString:='Driver={SQL Server};' + 'Server=bella;' + 'Database=nav;' + 'User ID=decrypter;' + 'Password=1234;'; IF ISCLEAR(lADOConnection) THEN CREATE(lADOConnection, FALSE, TRUE); lADOConnection.ConnectionString(ConnectionString); lADOConnection.Open; IF ISCLEAR(lADOCommand) THEN CREATE(lADOCommand, FALSE, TRUE); lvarActiveConnection := lADOConnection; lADOCommand.ActiveConnection := lvarActiveConnection; IF ISCLEAR(lADORecordset) THEN CREATE(lADORecordset, FALSE, TRUE); lADORecordset.ActiveConnection := lvarActiveConnection; SqlCommand := 'OPEN MASTER KEY DECRYPTION BY PASSWORD = ''blabla'' ' + 'ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY ' + 'CLOSE MASTER KEY '; lADOCommand.CommandText := SqlCommand; lADOCommand.CommandTimeout := 0; lADOCommand.Execute; SqlCommand := 'OPEN SYMMETRIC KEY TestTableKey DECRYPTION BY CERTIFICATE EncryptTestCert ' + 'declare @hexbin varbinary(max); ' + 'set @hexbin = ENCRYPTBYKEY(KEY_GUID(''TestTableKey''),N''1111''); '+ 'select ''0x'' + cast('''' as xml).value(''xs:hexBinary(sql:variable("@hexbin") )'', ''varchar(1024)'') as Column1;' + 'CLOSE SYMMETRIC KEY TestTableKey '; lADOCommand.CommandText := SqlCommand; lADOCommand.CommandTimeout := 0; lADOCommand.Execute; lADORecordset.Open('SET NOCOUNT ON'); lADORecordset.Open(lADOCommand); Value := lADORecordset.Fields.Item('Column1').Value; lADORecordset.Close; lADOConnection.Close; CLEAR(lADOConnection);
Answers
Independent Consultant/Developer
blog: https://dynamicsuser.net/nav/b/ara3n
I rewrote the code using ADO.NET and it worked.
I'm doing some of the same code, but I have trouble connecting to sql - on the SQLConnection.OPEN (it says "...network path not found").
I run NAV 2016
Thois is my connection sstring with dotnet
My connection string looks like this:
ConnectionString :='Data Source=mssql.xxxxx.dk;Initial Catalog=xxxxxxx;User Id=xxxx;Password=xxxxxxx';
The error appears in the
SQLConnection := SQLConnection.SqlConnection(ConnectionString);
SQLConnection.Open;
Hope to hear from you