共用方式為


SqlServices 類別

定義

支援安裝和移除 ASP.NET 功能的 SQL Server 資料庫項目。

public ref class SqlServices abstract sealed
public static class SqlServices
type SqlServices = class
Public Class SqlServices
繼承
SqlServices

範例

下列程式碼範例示範如何使用 SqlServices 類別。

using System;
using System.Web.Management;

namespace UsingSQLServices
{
    class UsingSqlServices
    {
        static void Main(string[] args)
        {
            try
            {
// Values to use.
string server = "ASPFeatureServer";
string database = "ASPFeatureDB";
string connectionString =
    "server=ASPFeatureServer, pooling=False, user=<user name>, password=<secure password>";
string user = "AspAdmin";
string password = "Secure Password"; // Use a sicure password.

// Install membership and personalization.
SqlServices.Install(database,
    SqlFeatures.Membership &
    SqlFeatures.Personalization,
    connectionString);

// Remove membership and personalization.
SqlServices.Uninstall(database,
    SqlFeatures.Membership &
    SqlFeatures.Personalization,
    connectionString);

// Install all features.
SqlServices.Install(server, database,
    SqlFeatures.All);

// Remove all features.
SqlServices.Uninstall(server, database,
    SqlFeatures.All);

// Install a custom session state database.
SqlServices.InstallSessionState(database,
    SessionStateType.Custom,
    connectionString);

// Remove a custom session state database.
SqlServices.UninstallSessionState(database,
    SessionStateType.Custom,
    connectionString);

// Install temporary session state.
SqlServices.InstallSessionState(server, null,
    SessionStateType.Temporary);

// Remove temporary session state.
SqlServices.UninstallSessionState(server, null,
    SessionStateType.Temporary);

// Install persisted session state.
SqlServices.InstallSessionState(server, user, password,
    null, SessionStateType.Persisted);

// Remove persisted session state.
SqlServices.UninstallSessionState(server, user, password,
    null, SessionStateType.Persisted);
            }
            catch (SqlExecutionException sqlExecutionException)
            {
Console.WriteLine(
    "An SQL execution exception occurred.");
Console.WriteLine();
Console.WriteLine("  Message: {0}",
    sqlExecutionException.Message);
Console.WriteLine("  Server: {0}",
    sqlExecutionException.Server);
Console.WriteLine("  Database: {0}",
    sqlExecutionException.Database);
Console.WriteLine("  Commands: {0}",
    sqlExecutionException.Commands);
Console.WriteLine("  SqlFile: {0}",
    sqlExecutionException.SqlFile);
Console.WriteLine("  Inner Exception: {0}",
    sqlExecutionException.Exception);
            }
            catch (Exception ex)
            {
Console.WriteLine("An unknown exception occurred.");
Console.WriteLine();
Console.WriteLine("  Message: {0}", ex.Message);
            }
        }
    }
}
Imports System.Web.Management

Module UsingSqlServices
    Sub Main()
        Try
' Values to use.
Dim server As String = "ASPFeatureServer"
Dim database As String = "ASPFeatureDB"
Dim connectionString As String = _
    "server=ASPFeatureServer, pooling=False, user=<user name>, password=<secure password>"
Dim user As String = "AspAdmin"
Dim password As String = "Secure Password"

' Install membership and personalization.
SqlServices.Install(database, _
    SqlFeatures.Membership And _
    SqlFeatures.Personalization, _
    connectionString)

' Remove membership and personalization.
SqlServices.Uninstall(database, _
    SqlFeatures.Membership And _
    SqlFeatures.Personalization, _
    connectionString)

' Install all features.
SqlServices.Install(server, database, _
    SqlFeatures.All)

' Remove all features.
SqlServices.Uninstall(server, database, _
    SqlFeatures.All)

' Install a custom session state database.
SqlServices.InstallSessionState(database, _
    SessionStateType.Custom, _
    connectionString)

' Remove a custom session state database.
SqlServices.UninstallSessionState(database, _
    SessionStateType.Custom, _
    connectionString)

' Install temporary session state.
SqlServices.InstallSessionState(server, Nothing, _
    SessionStateType.Temporary)

' Remove temporary session state.
SqlServices.UninstallSessionState(server, Nothing, _
    SessionStateType.Temporary)

' Install persisted session state.
SqlServices.InstallSessionState(server, user, password, _
    Nothing, SessionStateType.Persisted)

' Remove persisted session state.
SqlServices.UninstallSessionState(server, user, password, _
    Nothing, SessionStateType.Persisted)
        Catch sqlExecutionException As SqlExecutionException
Console.WriteLine( _
    "An SQL execution exception occurred.")
Console.WriteLine()
Console.WriteLine("  Message: {0}", _
    sqlExecutionException.Message)
Console.WriteLine("  Server: {0}", _
    sqlExecutionException.Server)
Console.WriteLine("  Database: {0}", _
    sqlExecutionException.Database)
Console.WriteLine("  Commands: {0}", _
    sqlExecutionException.Commands)
Console.WriteLine("  SqlFile: {0}", _
    sqlExecutionException.SqlFile)
Console.WriteLine("  Inner Exception: {0}", _
    sqlExecutionException.Exception)
        Catch ex As Exception
Console.WriteLine("An unknown exception occurred.")
Console.WriteLine()
Console.WriteLine("  Message: {0}", ex.Message)
        End Try
    End Sub
End Module

備註

您可以使用 SQL Server 資料庫來儲存 ASP.NET 功能的資訊,例如會話狀態、成員資格、Web 元件個人化和角色管理。 這些功能需要先正確設定連線的 SQL Server 資料庫,才能使用。 您可以使用 ASP.NET Register SQL 工具 (aspnet_regsql.exe) 執行此動作,也可以直接從程式代碼呼叫 類別公開 SqlServices 的靜態方法。

注意

使用 aspnet_regsql.exe 工具或 SqlServices 類別來設定資料庫,只會設定提供者將搭配適當的數據表、觸發程式和預存程式使用的資料庫。 以這種方式設定資料庫並不會修改組態檔,以指定這些功能會使用這些服務的 SQL 提供者。

方法

GenerateApplicationServicesScripts(Boolean, SqlFeatures, String)

產生選取功能的 SQL 指令碼。 這些指令碼會參考由資料庫參數指示的資料庫。

GenerateSessionStateScripts(Boolean, SessionStateType, String)

產生安裝或移除工作階段狀態的 SQL 指令碼。 此指令碼會參考由資料庫參數指示的資料庫。

Install(String, SqlFeatures, String)

在 SQL Server 資料庫上安裝選取 ASP.NET 服務的元件。

Install(String, String, SqlFeatures)

在 SQL Server 資料庫上安裝選取 ASP.NET 功能的元件。

Install(String, String, String, String, SqlFeatures)

在 SQL Server 資料庫上安裝選取 ASP.NET 功能的元件。

InstallSessionState(String, SessionStateType, String)

在 SQL Server 資料庫上安裝 ASP.NET 工作階段狀態的元件。

InstallSessionState(String, String, SessionStateType)

在 SQL Server 資料庫上安裝 ASP.NET 工作階段狀態的元件。

InstallSessionState(String, String, String, String, SessionStateType)

在 SQL Server 資料庫上安裝 ASP.NET 工作階段狀態的元件。

Uninstall(String, SqlFeatures, String)

從 SQL Server 資料庫移除選取 ASP.NET 功能的元件。

Uninstall(String, String, SqlFeatures)

從 SQL Server 資料庫移除選取 ASP.NET 功能的元件。

Uninstall(String, String, String, String, SqlFeatures)

從 SQL Server 資料庫移除選取 ASP.NET 功能的元件。

UninstallSessionState(String, SessionStateType, String)

從 SQL Server 資料庫移除 ASP.NET 工作階段狀態的元件。

UninstallSessionState(String, String, SessionStateType)

從 SQL Server 資料庫移除 ASP.NET 工作階段狀態的元件。

UninstallSessionState(String, String, String, String, SessionStateType)

從 SQL Server 資料庫移除 ASP.NET 工作階段狀態的元件。

適用於

另請參閱