共用方式為


使用加密

在 SMO 中,服務主要金鑰是由 ServiceMasterKey 物件表示,這可藉由 Server 物件的 ServiceMasterKey 屬性加以參考,並使用 Regenerate 方法來重新產生。

資料庫主要金鑰是由 MasterKey 物件表示。IsEncryptedByServer 屬性會指出是否利用服務主要金鑰來加密資料庫主要金鑰。每當資料庫主要金鑰變更時,master 資料庫中的加密副本就會自動更新。

您可以使用 DropServiceKeyEncryption 方法來卸除服務金鑰加密,然後使用密碼對資料庫主要金鑰進行加密。在該種情況下,您必須明確地開啟資料庫主要金鑰,才能存取該金鑰所保護的私密金鑰。

將資料庫附加到 SQL Server 的執行個體時,您必須為資料庫主要金鑰提供密碼,或執行 AddServiceKeyEncryption 方法,讓資料庫主要金鑰的未加密副本可以使用服務主要金鑰進行加密。建議使用此步驟,如此即不必明確地開啟資料庫主要金鑰。

Regenerate 方法會重新產生資料庫主要金鑰。當重新產生資料庫主要金鑰時,所有利用資料庫主要金鑰加密的金鑰都會解密,然後再利用新的資料庫主要金鑰來加密。DropServiceKeyEncryption 方法會利用服務主要金鑰移除資料庫主要金鑰的加密。AddServiceKeyEncryption 會導致主要金鑰的副本使用服務主要金鑰加密,然後同時儲存在目前的資料庫和 master 資料庫中。

在 SMO 中,憑證是由 Certificate 物件所表示。Certificate 物件的屬性可指定公開金鑰、主旨的名稱、有效期間和簽發者的相關資訊。存取此憑證的權限是藉由使用 Grant、Revoke 和 Deny 方法來控制。

範例

在下列的程式碼範例中,您必須選取用於建立應用程式的程式設計環境、程式設計範本和程式設計語言。如需詳細資訊,請參閱<如何:在 Visual Studio .NET 中建立 Visual Basic SMO 專案>和<如何:在 Visual Studio .NET 中建立 Visual C# SMO 專案>。

在 Visual Basic 中加入憑證

此程式碼範例會使用加密密碼來建立簡單的憑證。不同於其他物件,Create 方法有數個多載。範例中使用的多載會利用加密密碼來建立新的憑證。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'Define a Certificate object variable by supplying the parent database and name in the constructor.
Dim c As Certificate
c = New Certificate(db, "Test_Certificate")
'Set the start date, expiry date, and description.
c.StartDate = DateValue("January 01, 2007")
c.Subject = "This is a test certificate."
c.ExpirationDate = DateValue("January 01, 2008")
'Create the certificate on the instance of SQL Server by supplying the certificate password argument.
c.Create("pGFD4bb925DGvbd2439587y")

在 Visual C# 中加入憑證

此程式碼範例會使用加密密碼來建立簡單的憑證。不同於其他物件,Create 方法有數個多載。範例中使用的多載會利用加密密碼來建立新的憑證。

{
            //Connect to the local, default instance of SQL Server. 
            {
                Server srv = new Server();

                //Reference the AdventureWorks2008R2 database. 
                Database db = srv.Databases["AdventureWorks2008R2"];

                //Define a Certificate object variable by supplying the parent database and name in the constructor. 
                Certificate c = new Certificate(db, "Test_Certificate");

                //Set the start date, expiry date, and description. 
                System.DateTime dt;
                DateTime.TryParse("January 01, 2010", out dt);
                c.StartDate = dt;
                DateTime.TryParse("January 01, 2015", out dt);
                c.ExpirationDate = dt;
                c.Subject = "This is a test certificate.";
                //Create the certificate on the instance of SQL Server by supplying the certificate password argument. 
                c.Create("pGFD4bb925DGvbd2439587y");
            }
        } 

在 PowerShell 中加入憑證

此程式碼範例會使用加密密碼來建立簡單的憑證。不同於其他物件,Create 方法有數個多載。範例中使用的多載會利用加密密碼來建立新的憑證。

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2008R2
CD \sql\localhost\default\databases
$db = get-item AdventureWorks2008R2

#Create a certificate

$c = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Certificate -argumentlist $db, "Test_Certificate"
$c.StartDate = "January 01, 2010"
$c.Subject = "This is a test certificate."
$c.ExpirationDate = "January 01, 2015"

#Create the certificate on the instance of SQL Server by supplying the certificate password argument.
$c.Create("pGFD4bb925DGvbd2439587y")
 

請參閱

概念