Create SQL Server CREATE ASSEMBLY script automatically

When I use SSMS to create an assembly DLL, it generates a script as follows:

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

To generate this script automatically, I have to generate a part FROM 0x4D5A9..., is it a hex dump dll?

Is it possible to use an SQL function or should I generate this hex dump using a C # function?

+3
source share
1 answer

There is no SQL function. You can create it using some C #, for example:

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);
+4
source

All Articles