Bagikan melalui


Menggunakan Grup File dan File untuk Menyimpan Data

Berlaku untuk: SQL ServerAzure SQL Database Azure SQL Managed Instance Azure Synapse Analytics

File data digunakan untuk menyimpan file database. File data dibagi menjadi grup file. Objek Database memiliki FileGroups properti yang mereferensikan FileGroupCollection objek. Setiap FileGroup objek dalam koleksi tersebut memiliki Files properti . Properti ini mengacu pada DataFileCollection koleksi yang berisi semua file data milik database. Grup file terutama digunakan untuk mengelompokkan file bersama-sama yang digunakan untuk menyimpan objek database. Salah satu alasan untuk menyebarkan objek database ke beberapa file adalah dapat meningkatkan performa, terutama jika file disimpan di drive disk yang berbeda.

Setiap database yang dibuat secara otomatis memiliki grup file bernama "Primer" dan file data dengan nama yang sama dengan database. File dan grup tambahan dapat ditambahkan ke koleksi.

Contoh

Untuk contoh kode berikut, Anda harus memilih lingkungan pemrograman, templat pemrograman, dan bahasa pemrograman untuk membuat aplikasi Anda. Untuk informasi selengkapnya, lihat Membuat Proyek SMO Visual C# di Visual Studio .NET.

Menambahkan FileGroups dan DataFiles ke Database di Visual Basic

Grup file utama dan file data dibuat secara otomatis dengan nilai properti default. Contoh kode menentukan beberapa nilai properti yang dapat Anda gunakan. Jika tidak, nilai properti default digunakan.

'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()

Menambahkan FileGroups dan DataFiles ke Database di Visual C#

Grup file utama dan file data dibuat secara otomatis dengan nilai properti default. Contoh kode menentukan beberapa nilai properti yang dapat Anda gunakan. Jika tidak, nilai properti default digunakan.

{  
            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();  
        }  

Menambahkan FileGroups dan DataFiles ke Database di PowerShell

Grup file utama dan file data dibuat secara otomatis dengan nilai properti default. Contoh kode menentukan beberapa nilai properti yang dapat Anda gunakan. Jika tidak, nilai properti default digunakan.

# 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()  

Membuat, Mengubah, dan Menghapus File Log di Visual Basic

Contoh kode membuat LogFile objek, mengubah salah satu properti, lalu menghapusnya dari database.

'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()

Membuat, Mengubah, dan Menghapus File Log di Visual C#

Contoh kode membuat LogFile objek, mengubah salah satu properti, lalu menghapusnya dari database.

//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();  
  

Membuat, Mengubah, dan Menghapus File Log di PowerShell

Contoh kode membuat LogFile objek, mengubah salah satu properti, lalu menghapusnya dari database.

#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()  
  

Lihat Juga

FileGroup
File Database dan Grup File