CLR SQL Assembly: Get the Bytestream?

后端 未结 2 1786
广开言路
广开言路 2020-12-09 17:51

I have a SQL CLR dll I want to deploy, but have found you can embed the byte stream/varbinary_literal/ varbinary_expression/assembly bits into a text file to get around the

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 18:45

    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;
    

提交回复
热议问题