Search code examples
c#sql-serversql-server-2005sqlclr

SQL Server 2005 create assembly from stream with C#


Can i create an assembly from a stream much like MSSMS generated assembly script? I need to create an assembly from a DLL that is placed in a directory the SQL Server doesn't have access to.
The assembly installer does have access to the assembly though.
So my question is: can i read the assembly and generate a create from the stream read?

Thanks.


Solution

  • I think i figured it out..
    I'm using Tomalak's Byte Array to Hexadecimal method from this post.

    
         private string getAssemblyAsHex(string asmPath)
         {
            using (BinaryReader br = new BinaryReader(File.OpenRead(asmPath)))
            {
                byte[] buff = new byte[br.BaseStream.Length];
                br.Read(buff, 0, buff.Length - 1);
                return ByteArrayToString(buff);
            }
         }

        private static string ByteArrayToString(byte[] ba)
        {
            StringBuilder hex = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0:x2}", b);
            return hex.ToString();
        }
    

    Then i just concat this string to the CREATE ASSEMBLY statement.
    The code is written with a small DLL assembly in mind.