sqlserver create an assembly

sqlserver create an assembly

Suitable for:  yesSQL Server yesAzure SQL database noAzure SQL Data Warehousing noParallel Data Warehouse

Managed database objects (such as stored procedures or triggers) to be built, and then deployed to the unit referred to as program set. Managed DLL assemblies must be registered Microsoft to utilize the functionality the assembly provides SQL Server. To  register the assembly database, SQL Server, use the CREATE ASSEMBLY statement. This topic discusses how to use the CREATE ASSEMBLY statement to register the assembly in the database, and how to specify security settings for an assembly.

CREATE ASSEMBLY statement

CREATE ASSEMBLY statement is used to create an assembly in the database. Here is an example:

CREATE ASSEMBLY SQLCLRTest  
FROM 'C:\MyDBApp\SQLCLRTest.dll';  

FROM clause specifies the path name of the program you want to create the set. This path may be a Universal Naming Convention (UNC) path, it may be a computer local physical file path.

SQL Server  does not allow the use of the same name, culture, and public key registered to a different version of the assembly.

You can create reference assemblies other assemblies. When  when you create an assembly in SQL Server, SQL Server will create an assembly (If you have not created to be referenced in the database set) referenced by the root-level assembly.

It will be granted to database users or roles permission to create and then have the assembly in the database. In order to create an assembly, database user or role should have the CREATE ASSEMBLY permission.

Only when the following conditions are met, the assembly can be successfully reference other assemblies:

  • Assembly called or referenced owned by the same user or role.

  • Called or referenced assembly is created in the same database.

When you create a designated security assembly

When you create an assembly SQL Server database, you can specify three different codes which can run in one of the security levels: security, EXTERNAL_ACCESS, or UNSAFE. When the CREATE ASSEMBLY statement is run, it may lead not registered on the server assembly to perform some checks on the code assembly. For more information, see "simulation examples on CodePlex .

Safety is the default permission set for most programs. To specify a given level of security, you can modify the syntax CREATE ASSEMBLY statement is as follows:

CREATE ASSEMBLY SQLCLRTest  
FROM 'C:\MyDBApp\SQLCLRTest.dll'  
WITH PERMISSION_SET = SAFE;  

It is also possible to create a set of security permissions an assembly with a third row only by omitting the above code:

CREATE ASSEMBLY SQLCLRTest  
FROM 'C:\MyDBApp\SQLCLRTest.dll';  

As security permissions set to run under the centralized procedure code, it can only be executed within a managed program to provide data access servers in the computing and adoption process.

UNSAFE assemblies and create EXTERNAL_ACCESS

EXTERNAL_ACCESS resolve the code needs to access the server, such as program resources other than files, network, registry and environment variables. As long as the server to access external resources, it will simulate the security context of the user's call managed code.

Unsafe code permission for these cases, the assembly is not verifiable as safe or require further access to restricted resources, such as MicrosoftWin32 API.

To create EXTERNAL_ACCESS or UNSAFE assembly in SQL Server, you must meet one of the following two conditions:

  1. The assembly is strong name signed or with a certificate Authenticode signature. Within this created a strong name (or certificate) SQL Server as an asymmetric key (or certificate) and has a corresponding login name EXTERNAL ACCESS ASSEMBLY permission (for external access assemblies) or UNSAFE ASSEMBLY permission (for unsafe assemblies ).

  2. Database owner (DBO) has EXTERNAL ACCESS ASSEMBLY (for external access assemblies) or UNSAFE ASSEMBLY (for UNSAFE assemblies) permission, and the database has TRUSTWORTHY database property set to ON.

Look forward to your feedback: If you find incorrect or outdated content (such as steps or sample code) In this article, please let us know. You can click the bottom of this page "Feedback" section of the "page" button. We usually read about feedback in the next day of each SQL. Thank you.

When the assembly is loaded (including execution), and checks two conditions listed above. At least one of these conditions must be met in order to load the assembly.

We recommend TRUSTWORTHY database property on a database not be set to ON only to run common language runtime (CLR) code in the server process. But to create an asymmetric key recommendations by the assembly file in the master database. Then, you must create a login mapped to this asymmetric key, and the login name must be granted EXTERNAL ACCESS ASSEMBLY or UNSAFE ASSEMBLY permissions.

The following Transact-SQL statement will execute create an asymmetric key, login name mapped to this key, and then grant the required permissions to step EXTERNAL_ACCESS login. You must run the following  Transact-SQL statements before you run the CREATE ASSEMBLY statement.

USE master;   
GO    
  
CREATE ASYMMETRIC KEY SQLCLRTestKey FROM EXECUTABLE FILE = 'C:\MyDBApp\SQLCLRTest.dll'     
CREATE LOGIN SQLCLRTestLogin FROM ASYMMETRIC KEY SQLCLRTestKey     
GRANT EXTERNAL ACCESS ASSEMBLY TO SQLCLRTestLogin;   
GO   

 Remark

You must create a new login name to be associated with an asymmetric key. This login is only used to grant permission; user does not have to be associated with or used in an application.

To create an external access assemblies, the creator needs to have external access. This right specifies when creating assemblies:

CREATE ASSEMBLY SQLCLRTest  
FROM 'C:\MyDBApp\SQLCLRTest.dll'  
WITH PERMISSION_SET = EXTERNAL_ACCESS;  

The following Transact-SQL statement will execute create an asymmetric key, login name mapped to this key, and then grant the required steps to UNSAFE permission to login. You must run the following  Transact-SQL statements before you run the CREATE ASSEMBLY statement.

USE master;   
GO    
  
CREATE ASYMMETRIC KEY SQLCLRTestKey FROM EXECUTABLE FILE = 'C:\MyDBApp\SQLCLRTest.dll';     
CREATE LOGIN SQLCLRTestLogin FROM ASYMMETRIC KEY SQLCLRTestKey ;    
GRANT UNSAFE ASSEMBLY TO SQLCLRTestLogin ;  
GO  

Specifies the assembly is loaded with UNSAFE permissions, specify when the assembly is loaded UNSAFE permission sets to the server:

CREATE ASSEMBLY SQLCLRTest  
FROM 'C:\MyDBApp\SQLCLRTest.dll'  
WITH PERMISSION_SET = UNSAFE;  

For more detailed information about each setting permissions, see CLR Integration Security .

See

Management CLR integration assemblies  
change the assembly  
remove an assembly  
CLR Integration Code Access Security  
TRUSTWORTHY database property  
allows the caller partially trusted

Guess you like

Origin www.cnblogs.com/buffercache/p/11446231.html