3

When I use SSMS to create an assembly dll, it generates a script like this :

CREATE ASSEMBLY [SqlFunctions]
AUTHORIZATION [dbo]
FROM 0x4D5A9...
WITH PERMISSION_SET = UNSAFE

To generate this script automatically, I must generate the FROM 0x4D5A9... part, is it the hex dump of the dll ?

Is it possible using an SQL function or should I generate this hex dump with a c# function ?

1 Answer 1

4

There's no SQL function. You can create it using some C# like this:

Assembly clrAssembly = ...;
const string createTemplate = @"CREATE ASSEMBLY [{0}] AUTHORIZATION [dbo] FROM 0x{1};";

var bytes = new StringBuilder();
using (var dll = File.OpenRead(clrAssembly.Location))
{
int @byte;
while ((@byte = dll.ReadByte()) >= 0)
    bytes.AppendFormat("{0:X2}", @byte);
}

var sql = string.Format(createTemplate, clrAssembly.GetName().Name, bytes);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.