SqlServices 類別

定義

支援安裝和移除 ASP.NET 功能的 SQL Server 資料庫項目。Supports installing and removing the SQL Server database elements of ASP.NET features.

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

範例

下列程式碼範例示範如何使用 SqlServices 類別。The following code example shows how to use the SqlServices class.

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 元件個人化和角色管理。You can use SQL Server databases to store information for ASP.NET features such as session state, membership, Web parts personalization, and role management. 這些功能需要在使用之前正確設定其連接的 SQL Server 資料庫。These features require that the SQL Server database to which they are connected be properly configured before use. 您可以使用 ASP.NET Register SQL 工具(aspnet_regsql .exe)來執行這項操作,也可以直接從程式碼呼叫 SqlServices 類別所公開的靜態方法。You can use the ASP.NET Register SQL tool (aspnet_regsql.exe) to do this, or you can call the static methods exposed by the SqlServices class directly from your code.

注意

使用 aspnet_regsql .exe 工具或 SqlServices 類別來設定資料庫時,只會將提供者將搭配適當資料表、觸發程式和預存程式使用的資料庫。Using the aspnet_regsql.exe tool or the SqlServices class to configure the database only sets up the database that the providers will use with the proper tables, triggers, and stored procedures. 以這種方式設定資料庫並不會修改設定檔,以指定這些功能會使用這些服務的 SQL 提供者。Configuring the database in this way does not modify configuration files to specify that these features use the SQL Providers for these services.

方法

GenerateApplicationServicesScripts(Boolean, SqlFeatures, String)

產生選取功能的 SQL 指令碼。Generates the SQL scripts for the selected features. 這些指令碼會參考由資料庫參數指示的資料庫。The scripts will reference the database indicated by the database parameter.

GenerateSessionStateScripts(Boolean, SessionStateType, String)

產生安裝或移除工作階段狀態的 SQL 指令碼。Generates the SQL script for installing or removing session state. 此指令碼會參考由資料庫參數指示的資料庫。The script will reference the database indicated by the database parameter.

Install(String, SqlFeatures, String)

在 SQL Server 資料庫上安裝選取 ASP.NET 服務的元件。Installs components for selected ASP.NET services on a SQL Server database.

Install(String, String, SqlFeatures)

在 SQL Server 資料庫上安裝選取 ASP.NET 功能的元件。Installs components for selected ASP.NET features on a SQL Server database.

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

在 SQL Server 資料庫上安裝選取 ASP.NET 功能的元件。Installs components for selected ASP.NET features on a SQL Server database.

InstallSessionState(String, SessionStateType, String)

在 SQL Server 資料庫上安裝 ASP.NET 工作階段狀態的元件。Installs components for ASP.NET session state on a SQL Server database.

InstallSessionState(String, String, SessionStateType)

在 SQL Server 資料庫上安裝 ASP.NET 工作階段狀態的元件。Installs components for ASP.NET session state on a SQL Server database.

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

在 SQL Server 資料庫上安裝 ASP.NET 工作階段狀態的元件。Installs components for ASP.NET session state on a SQL Server database.

Uninstall(String, SqlFeatures, String)

從 SQL Server 資料庫移除選取 ASP.NET 功能的元件。Removes components for selected ASP.NET features from a SQL Server database.

Uninstall(String, String, SqlFeatures)

從 SQL Server 資料庫移除選取 ASP.NET 功能的元件。Removes components for selected ASP.NET features from a SQL Server database.

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

從 SQL Server 資料庫移除選取 ASP.NET 功能的元件。Removes components for selected ASP.NET features from a SQL Server database.

UninstallSessionState(String, SessionStateType, String)

從 SQL Server 資料庫移除 ASP.NET 工作階段狀態的元件。Removes components for ASP.NET session state from a SQL Server database.

UninstallSessionState(String, String, SessionStateType)

從 SQL Server 資料庫移除 ASP.NET 工作階段狀態的元件。Removes components for ASP.NET session state from a SQL Server database.

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

從 SQL Server 資料庫移除 ASP.NET 工作階段狀態的元件。Removes components for ASP.NET session state from a SQL Server database.

適用於

另請參閱