在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命令来备份指定的表到指定的文件路径。