代码之家  ›  专栏  ›  技术社区  ›  OMG Ponies

CLR SQL程序集:获取Bytestream?

  •  14
  • OMG Ponies  · 技术社区  · 14 年前

    我有一个SQLCLRDLL我想部署,但发现你可以嵌入字节流/varbinary\u literal/varbinary\u expression/assembly位到一个文本文件中,以摆脱打包dll和确保用户可以访问它的麻烦 CREATE ASSEMBLY command

    Load() .

    2 回复  |  直到 6 年前
        1
  •  23
  •   Sander Rijken    14 年前

    它只是dll的十六进制表示。这一点应该可以做到:

        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();
        }
    

    string hexString = GetHexString(assemblyPath);
    string sql = "CREATE ASSEMBLY [" + assemblyName + "] FROM " + hexString + 
                 " WITH PERMISSION_SET = " + somePermissionSet;
    
        2
  •  7
  •   Frédéric    6 年前

    找到 here varbinary 仅通过使用SQLServerManagementStudio(SSMS)和本地SQLServer实例,无需自定义代码即可生成。

    1. create alter 在数据库中使用程序集在本地SQL Server上的本地路径创建程序集。

      use yourBase
      go
      create assembly YourAssemblySqlName from N'YourLocalPath\YourAssemblyFile.dll'
      go
      
    2. 在对象资源管理器中浏览到程序集。

      Browsing to assembly

    3. Scripting the assembly

    可变长度 .