在C#中,可以使用SQL Server的BACKUP DATABASE語句來備份數據庫中的表。以下是一個示例代碼:
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "your_connection_string_here";
string tableName = "your_table_name_here";
string backupPath = "your_backup_path_here";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = $"BACKUP DATABASE [YourDatabaseName] TABLE {tableName} TO DISK = '{backupPath}'";
SqlCommand command = new SqlCommand(query, connection);
command.ExecuteNonQuery();
Console.WriteLine("Table backup complete.");
}
}
}
請注意,需要將代碼中的your_connection_string_here
替換為您的數據庫連接字符串,your_table_name_here
替換為您要備份的表名,your_backup_path_here
替換為備份文件的路徑。此代碼將執行一個SQL命令來備份指定的表到指定的文件路徑。