次の方法で共有


ファイルグループとファイルを使用してデータを格納する方法

データ ファイルは、データベース ファイルを格納するために使用されます。 データ ファイルはファイル グループに分割されます。 Database オブジェクトには、FileGroupCollection オブジェクトを参照するFileGroups プロパティがあります。 そのコレクション内の各 FileGroup オブジェクトには、 Files プロパティがあります。 このプロパティは、データベースに属するすべてのデータ ファイルを含む DataFileCollection コレクションを参照します。 ファイル グループは、データベース オブジェクトの格納に使用されるファイルをまとめてグループ化するために主に使用されます。 データベース オブジェクトを複数のファイルに分散させる理由の 1 つは、特にファイルが異なるディスク ドライブに格納されている場合にパフォーマンスを向上できることです。

自動的に作成されるすべてのデータベースには、"Primary" という名前のファイル グループと、データベースと同じ名前のデータ ファイルがあります。 コレクションには、追加のファイルとグループを追加できます。

例示

次のコード例では、アプリケーションを作成するために、プログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「 Visual Studio .NET で Visual Basic SMO プロジェクトを作成する」および「Visual Studio .NETで Visual C# SMO プロジェクトを作成する」を参照してください。

Visual Basic でのデータベースへの FileGroups と DataFiles の追加

プライマリ ファイル グループとデータ ファイルは、既定のプロパティ値を使用して自動的に作成されます。 コード例では、使用できるプロパティ値をいくつか指定します。 それ以外の場合は、既定のプロパティ値が使用されます。

Visual C でのデータベースへの FileGroups と DataFiles の追加#

プライマリ ファイル グループとデータ ファイルは、既定のプロパティ値を使用して自動的に作成されます。 コード例では、使用できるプロパティ値をいくつか指定します。 それ以外の場合は、既定のプロパティ値が使用されます。

{  
            Server srv = new Server();  
            //Reference the AdventureWorks2012 database.   
            Database db = default(Database);  
            db = srv.Databases["AdventureWorks2012"];  
            //Define a FileGroup object called SECONDARY on the database.   
            FileGroup fg1 = default(FileGroup);  
            fg1 = new FileGroup(db, "SECONDARY");  
            //Call the Create method to create the file group on the instance of SQL Server.   
            fg1.Create();  
            //Define a DataFile object on the file group and set the FileName property.   
            DataFile df1 = default(DataFile);  
            df1 = new DataFile(fg1, "datafile1");  
            df1.FileName = "c:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\datafile2.ndf";  
            //Call the Create method to create the data file on the instance of SQL Server.   
            df1.Create();  
        }  

PowerShell でのデータベースへの FileGroups と DataFiles の追加

プライマリ ファイル グループとデータ ファイルは、既定のプロパティ値を使用して自動的に作成されます。 コード例では、使用できるプロパティ値をいくつか指定します。 それ以外の場合は、既定のプロパティ値が使用されます。

# Set the path context to the local, default instance of SQL Server.  
CD \sql\localhost\default\Databases\  
  
#And the database object corresponding to AdventureWorks2012.  
$db = get-item AdventureWorks2012  
  
#Create a new filegroup  
$fg1 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Filegroup -argumentlist $db, "SECONDARY"  
$fg1.Create()  
  
#Define a DataFile object on the file group and set the FileName property.   
$df1 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.DataFile -argumentlist $fg1, "datafile1"  
  
#Make sure to have a directory created to hold the designated data file  
$df1.FileName = "c:\\TestData\\datafile2.ndf"  
  
#Call the Create method to create the data file on the instance of SQL Server.   
$df1.Create()  

Visual Basic でのログ ファイルの作成、変更、および削除

このコード例では、 LogFile オブジェクトを作成し、プロパティの 1 つを変更してから、データベースから削除します。

Visual C でのログ ファイルの作成、変更、および削除#

このコード例では、 LogFile オブジェクトを作成し、プロパティの 1 つを変更してから、データベースから削除します。

//Connect to the local, default instance of SQL Server.   
            Server srv = new Server();  
            //Reference the AdventureWorks2012 database.   
            Database db = default(Database);  
            db = srv.Databases["AdventureWorks2012"];  
            //Define a LogFile object and set the database, name, and file name properties in the constructor.   
            LogFile lf1 = default(LogFile);  
            lf1 = new LogFile(db, "logfile1", "c:\\Program Files\\Microsoft SQL Server\\MSSQL.10_50.MSSQLSERVER\\MSSQL\\Data\\logfile1.ldf");  
            //Set the file growth to 6%.   
            lf1.GrowthType = FileGrowthType.Percent;  
            lf1.Growth = 6;  
            //Run the Create method to create the log file on the instance of SQL Server.   
            lf1.Create();  
            //Alter the growth percentage.
            lf1.Growth = 7;  
            lf1.Alter();  
            //Remove the log file.
            lf1.Drop();

PowerShell でのログ ファイルの作成、変更、および削除

このコード例では、 LogFile オブジェクトを作成し、プロパティの 1 つを変更してから、データベースから削除します。

#Load the assembly containing the enums used in this example  
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum")  
  
# Set the path context to the local, default instance of SQL Server.  
CD \sql\localhost\default\Databases\  
  
#And the database object corresponding to AdventureWorks2012  
$db = get-item AdventureWorks2012  
  
#Create a filegroup  
$fg1 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Filegroup -argumentlist $db, "Secondary"  
  
#Call the Create method to create the file group on the instance of SQL Server.   
$fg1.Create()  
  
#Define a LogFile object on the file group and set the FileName property.   
$lf1 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.LogFile -argumentlist $db, "LogFile2"  
  
#Set a location for it - make sure the directory exists  
$lf1.FileName = "logfile1", "c:\\Program Files\\Microsoft SQL Server\\MSSQL.10_50.MSSQLSERVER\\MSSQL\\Data\\logfile1.ldf"  
  
#Set file growth to 6%  
$lf1.GrowthType = [Microsoft.SqlServer.Management.Smo.FileGrowthType]::Percent  
$lf1.Growth = 6.0  
  
#Call the Create method to create the data file on the instance of SQL Server.   
$lf1.Create()  
  
#Alter a value and drop the log file  
$lf1.Growth = 7.0  
$lf1.Alter()  
$lf1.Drop()

こちらもご覧ください

FileGroup
データベース ファイルとファイル グループ