共用方式為


使用檔案群組和檔案來儲存資料

資料檔案可用於儲存資料庫檔案。資料檔案被分成檔案群組。Database 物件具有 FileGroups 屬性,這個屬性會參考 FileGroupCollection 物件。該集合中的每個 FileGroup 物件都具有 Files 屬性。這個屬性會參考 DataFileCollection 集合,其中包含資料庫所屬的所有資料檔。檔案群組的主要功能是將用於儲存資料庫物件的檔案放入群組。將一個資料庫物件散佈到數個檔案的其中一個理由就是改善效能,特別是如果檔案儲存在不同的磁碟機上。

每個自動建立的資料庫都具有名為 "Primary" 的檔案群組以及與資料庫同名的資料檔。可以在集合中加入其他檔案和群組。

範例

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

在 Visual Basic 中將 FileGroups 和 DataFiles 加入至資料庫

主要檔案群組和資料檔會使用預設的屬性值自動建立。程式碼範例會指定某些可以使用的某些屬性值;否則,會使用預設屬性值。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a FileGroup object called SECONDARY on the database.
Dim fg1 As 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.
Dim df1 As 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()

在 Visual C# 中將 FileGroups 和 DataFiles 加入至資料庫

主要檔案群組和資料檔會使用預設的屬性值自動建立。程式碼範例會指定某些可以使用的某些屬性值;否則,會使用預設屬性值。

{ 
Server srv = default(Server); 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db = default(Database); 
db = srv.Databases("AdventureWorks"); 
//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(); 
}

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

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

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a LogFile object and set the database, name, and file name properties in the constructor.
Dim lf1 As LogFile
lf1 = New LogFile(db, "logfile1", "c:\Program Files\Microsoft SQL Server\MSSQL.1\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()

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

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

{
//Connect to the local, default instance of SQL Server. 
Server srv = default(Server); 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db = default(Database); 
db = srv.Databases("AdventureWorks"); 
//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.1\\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(); 
} 

請參閱

參考

概念