Udostępnij za pośrednictwem


Używanie grup plików i plików do przechowywania danych

Dotyczy:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsBaza danych SQL w usłudze Microsoft Fabric (wersja zapoznawcza)

Pliki danych są używane do przechowywania plików bazy danych. Pliki danych są podzielone na grupy plików. Obiekt Database ma właściwość odwołującą FileGroupsFileGroupCollection się do obiektu. Każdy FileGroup obiekt w tej kolekcji ma Files właściwość . Ta właściwość odwołuje się do DataFileCollection kolekcji zawierającej wszystkie pliki danych należące do bazy danych. Grupa plików jest głównie używana do grupowania plików, które są używane do przechowywania obiektu bazy danych. Jednym z powodów rozprzestrzeniania obiektu bazy danych na kilka plików jest to, że może zwiększyć wydajność, zwłaszcza jeśli pliki są przechowywane na różnych dyskach.

Każda utworzona baza danych automatycznie ma grupę plików o nazwie "Podstawowa" i plik danych o takiej samej nazwie jak baza danych. Do kolekcji można dodać dodatkowe pliki i grupy.

Examples

W poniższych przykładach kodu należy wybrać środowisko programowania, szablon programowania i język programowania, aby utworzyć aplikację. Aby uzyskać więcej informacji, zobacz Create a Visual C# SMO Project in Visual Studio .NET(Tworzenie projektu SMO w programie Visual Studio .NET).

Dodawanie grup plików i plików DataFiles do bazy danych w Visual Basic

Podstawowa grupa plików i plik danych są tworzone automatycznie z domyślnymi wartościami właściwości. Przykład kodu określa niektóre wartości właściwości, których można użyć. W przeciwnym razie są używane domyślne wartości właściwości.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'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()

Dodawanie grup plików i plików datafile do bazy danych w programie Visual C#

Podstawowa grupa plików i plik danych są tworzone automatycznie z domyślnymi wartościami właściwości. Przykład kodu określa niektóre wartości właściwości, których można użyć. W przeciwnym razie są używane domyślne wartości właściwości.

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

Dodawanie grup plików i plików datafile do bazy danych w programie PowerShell

Podstawowa grupa plików i plik danych są tworzone automatycznie z domyślnymi wartościami właściwości. Przykład kodu określa niektóre wartości właściwości, których można użyć. W przeciwnym razie są używane domyślne wartości właściwości.

# Set the path context to the local, default instance of SQL Server.  
CD \sql\localhost\default\Databases\  
  
#And the database object corresponding to AdventureWorks2022.  
$db = get-item AdventureWorks2022  
  
#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()  

Tworzenie, zmienianie i usuwanie pliku dziennika w Visual Basic

Przykładowy kod tworzy LogFile obiekt, zmienia jedną z właściwości, a następnie usuwa go z bazy danych.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'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()

Tworzenie, zmienianie i usuwanie pliku dziennika w programie Visual C#

Przykładowy kod tworzy LogFile obiekt, zmienia jedną z właściwości, a następnie usuwa go z bazy danych.

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

Tworzenie, zmienianie i usuwanie pliku dziennika w programie PowerShell

Przykładowy kod tworzy LogFile obiekt, zmienia jedną z właściwości, a następnie usuwa go z bazy danych.

#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 AdventureWorks2022  
$db = get-item AdventureWorks2022  
  
#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()  
  

See Also

FileGroup
pliki bazy danych i grupy plików