如何从 C# 代码创建 SQL 服务器主密钥和证书

本文关键字:服务器 主密钥 证书 SQL 创建 代码 | 更新日期: 2023-09-27 18:36:06

我可以按照sql命令从sql服务器执行中创建它,但是有没有办法使用C#代码执行此命令?

SQL 命令 ...

USE master;
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'rajesh';
Go
Use Master
    BACKUP MASTER KEY TO FILE = 'C:'Users'HP USER'Desktop'Encrypt'KEYs' 
    ENCRYPTION BY PASSWORD = 'rajesh'
GO 
Use Master
      CREATE CERTIFICATE mytestcert
      WITH SUBJECT = 'Mytestcertificate'
Use Master
    BACKUP CERTIFICATE mytestcert TO FILE = 'C:'Users'HP USER'Desktop'Encrypt'mytestcert.cer'
GO
CREATE DATABASE dbShop ;
GO  
USE dbShop
GO
CREATE DATABASE ENCRYPTION KEY 
  WITH ALGORITHM = AES_256
  ENCRYPTION BY SERVER CERTIFICATE [mytestcert]
ALTER DATABASE test
  SET ENCRYPTION ON;

如何从 C# 代码创建 SQL 服务器主密钥和证书

可以将命令放在存储的进程中,然后从 C# 执行存储的进程。