TB007
Member
- Joined
- Aug 13, 2022
- Messages
- 24
- Programming Experience
- Beginner
Prensently I'm using the below approach to copy multiple database files (SQLite) from one location to another and overwrite them if the database/databases already exists:
The CopyDatabase method is as under:
Can this method be made more efficient and/or less crash/error proof ?
Are there other/better ways to do this ?
C#:
string sourceDir = @"D:\test\from\";
string backupDir = @"D:\test\to\";
string[] databaseFiles = Directory.GetFiles(sourceDir, "*.db");
foreach (string databaseFile in databaseFiles)
{
string fileName = databaseFile.Substring(sourceDir.Length);
CopyDatabase(Path.Combine(sourceDir, fileName), Path.Combine(backupDir, fileName));
}
The CopyDatabase method is as under:
C#:
public static void CopyDatabase(string sourceFile, string destFile)
{
using (SQLiteConnection source = new SQLiteConnection(String.Format("Data Source = {0}", sourceFile)))
using (SQLiteConnection destination = new SQLiteConnection(String.Format("Data Source = {0}", destFile)))
{
source.Open();
destination.Open();
source.BackupDatabase(destination, "main", "main", -1, null, -1);
}
}
Can this method be made more efficient and/or less crash/error proof ?
Are there other/better ways to do this ?