Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create SQL Server CREATE ASSEMBLY script automatically

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 ?

like image 301
psadac Avatar asked Dec 02 '25 14:12

psadac


1 Answers

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);
like image 173
Tim Rogers Avatar answered Dec 04 '25 05:12

Tim Rogers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!