CLR SQL Assembly: Get the Bytestream?

谁说我不能喝 提交于 2019-11-28 09:07:27

It's just a hex representation of the dll. This bit should do the trick:

    static string GetHexString(string assemblyPath)
    {
        if (!Path.IsPathRooted(assemblyPath))
            assemblyPath = Path.Combine(Environment.CurrentDirectory, assemblyPath);

        StringBuilder builder = new StringBuilder();
        builder.Append("0x");

        using (FileStream stream = new FileStream(assemblyPath,
              FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            int currentByte = stream.ReadByte();
            while (currentByte > -1)
            {
                builder.Append(currentByte.ToString("X2", CultureInfo.InvariantCulture));
                currentByte = stream.ReadByte();
            }
        }

        return builder.ToString();
    }

You should use the resulting string like so:

string hexString = GetHexString(assemblyPath);
string sql = "CREATE ASSEMBLY [" + assemblyName + "] FROM " + hexString + 
             " WITH PERMISSION_SET = " + somePermissionSet;

Found here, the varbinary can be generated without custom code for generating it, only by using SQL Server Management Studio (SSMS) and a local SQL Server instance.

  1. create or alter your assembly in your database using its local path on your local SQL Server.

    use yourBase
    go
    create assembly YourAssemblySqlName from N'YourLocalPath\YourAssemblyFile.dll'
    go
    
  2. Browse to your assembly in Object Explorer.

  3. Script its creation.

And SSMS gives you the varbinary.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!