简体   繁体   中英

Create an Asymmetric key in a Visual Studio 2012 Sql Server project

I'm developing a Visual Studio 2012 SQL Server project with four CLR stored procedures. I'm going to use it on a SQL Server 2012 server.

Before that project I have another one, only with the CLR Stored Procedure, and I use this sql to add it to my database:

 -- #####################################################################
 -- Add SQL CLR Procedures.
 -- #####################################################################

Use master
go

-- Configure CLR execution on SQL Server.
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO

-- For stored procedure dll...
CREATE ASYMMETRIC KEY CodeServerAK FROM EXECUTABLE FILE = 'D:\MyPath\bin\Release\CodeServerDB.dll'
CREATE LOGIN SQLCLRCodeServerSP FROM ASYMMETRIC KEY CodeServerAK
GRANT EXTERNAL ACCESS ASSEMBLY TO SQLCLRCodeServerSP 
go

USE MyDB
GO

-- Create users from assemblies' login name.
CREATE USER SQLCLRCodeServerSPUser FOR LOGIN SQLCLRCodeServerSP
GO

USE MyDB
GO

CREATE ASSEMBLY CodeServerDB FROM  'D:\MyPath\bin\Release\CodeServerDB.dll'
WITH PERMISSION_SET=EXTERNAL_ACCESS
GO

But now I have a problem: I don't know how to create the Asymmetric key CodeServerAK because now I don't have the path to the dll.

How can I create the asymmetric key?

I think I don't need to use this sql:

CREATE ASSEMBLY CodeServerDB FROM  'D:\MyPath\bin\Release\CodeServerDB.dll'
    WITH PERMISSION_SET=EXTERNAL_ACCESS
    GO

I have publish the VS project on SQL Server and the assembly is there.

By the way, the assembly is signed.

As an inelegant solution I have a pre build step which copies the key file from a path relative to the project to a network share which is accessible by both the build machine and the target sql server eg copy /Y "$(ProjectDir)bin\\Release\\CodeServerDB.dll" "\\myserver\\share\\path" .

You can then update the CREATE ASYMMETRIC KEY command to reference the networked share.

I haven't discovered yet how to have Visual Studio build and deploy the asymmetric key for automatically.

SideNote: You can have Visual Studio build and deploy the assembly for you without need to refer to the binary dll - check the Generate DDL option on the Project Settings SQLCLR tab, the CREATE ASSEMBLY statement is generated with encoded binary

CREATE ASSEMBLY [My.SQLServer.SQLCLR]
    AUTHORIZATION [dbo]
    FROM 0x4D5A900003{...}0000000000000
    WITH PERMISSION_SET = EXTERNAL_ACCESS;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM