2016-11-19 131 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

相关问题