2016-11-19 60 views
0

使用下面的代碼,我該如何在SQLiteConnetion對象中設置代碼?C# - 如何從一個路徑複製SQLite數據庫並將其粘貼到另一個路徑?

public SQLiteConnection dbConnection = new SQLiteConnection();

string fileName = "test.s3db"; 
     string sourcePath = @"E:\File\DMS\DAL\Model"; 
     string targetPath = @"C:\ProgramData\CompanyName\appName"; 
     string sourceFile = System.IO.Path.Combine(sourcePath, fileName); 
     string destFile = System.IO.Path.Combine(targetPath, fileName); 
     if (!System.IO.Directory.Exists(targetPath)) 
     { 
      System.IO.Directory.CreateDirectory(targetPath); 

     } 
     System.IO.File.Copy(sourceFile, destFile, true); 
     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 
     } 

我想,如果路徑不存在自動創建一個數據庫路徑。

dbConnection = ?? 
+0

你能否澄清你是什麼_表示「我怎麼可以設置SQLiteConnetion對象的代碼」 _?你的代碼似乎是正確的。你能提供一個不工作的代碼片段嗎? –

+0

@ botond.botos嗨其實我正面臨的問題是,請看看這個網址:http://stackoverflow.com/questions/40689467/where-to-store-my-windows-applications-data。然後,我想創建一個默認路徑,當在任何本地計算機上安裝我的應用程序,同時在wpf中創建安裝文件時c# – lashja

+0

有一種不同的方法描述在http://stackoverflow.com/questions/15292880/create-sqlite-database-and-table 。它不會複製數據庫文件,而是在運行時創建它。 –

回答

1

我有這樣的方法複製數據庫:

public static void BackupDatabase(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); 
    } 
} 
+0

嗨。這是我的數據庫字符串fileName =「test.s3db」; – lashja

+1

@AbhilashJA所以你應該用'test.s3db'替換'YourDatabase.db'。 –

+0

如何找到這兩條路徑? string sourcePath = @「E:\ File \ DMS \ DAL \ Model」; string targetPath = @「C:\ ProgramData \ CompanyName \ appName」; 使用此代碼? – lashja

相關問題