阅读量:0
在C#中使用OleDbConnection进行数据备份和恢复操作需要使用SQL语句来实现。以下是一个简单的示例代码:
using System; using System.Data.OleDb; namespace DataBackupRestore { class Program { static void Main(string[] args) { string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\data\\database.mdb"; // 备份数据 using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); string backupPath = "C:\\data\\backup.mdb"; string backupQuery = $"SELECT * INTO [MS Access;Database={backupPath}].* FROM TableName;"; using (OleDbCommand command = new OleDbCommand(backupQuery, connection)) { command.ExecuteNonQuery(); } } // 恢复数据 using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); string restorePath = "C:\\data\\restore.mdb"; string restoreQuery = $"SELECT * INTO TableName FROM [MS Access;Database={restorePath}].TableName;"; using (OleDbCommand command = new OleDbCommand(restoreQuery, connection)) { command.ExecuteNonQuery(); } } } } }
在上面的示例中,首先连接到数据库,然后使用SELECT INTO语句将数据备份到指定路径的数据库文件中。然后再次连接到原始数据库,使用SELECT INTO语句从备份文件中恢复数据。请确保替换示例代码中的数据库路径和表名为实际需要备份和恢复的数据库和表名。