共用方式為


使用檔案群組和檔案來儲存數據

數據檔可用來儲存資料庫檔案。 數據檔會細分為檔案群組。 物件 Database 具有 FileGroups 參考 FileGroupCollection 對象的屬性。 FileGroup該集合中的每個物件都有 Files 屬性。 這個屬性是指 DataFileCollection 集合,其中包含屬於資料庫的所有數據檔。 檔案群組主要用來將用來儲存資料庫對象的檔案群組在一起。 將資料庫物件分散到數個檔案的其中一個原因是它可以改善效能,特別是當檔案儲存在不同的磁碟驅動器上時。

每個自動建立的資料庫都有名為 「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 建立 對象、變更其中一個屬性,然後將它從資料庫移除。

在 Visual C 中建立、改變和移除記錄檔#

程式代碼範例會 LogFile 建立 對象、變更其中一個屬性,然後將它從資料庫移除。

//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 建立 對象、變更其中一個屬性,然後將它從資料庫移除。

#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
資料庫檔案與檔案群組