0

Using below code, how can I set the code in SQLiteConnetion object ?

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);
        }

I want to automatically create a DB path if the path does not exist.

dbConnection = ??
lashja
  • 493
  • 10
  • 21
  • Could you clarify what do you mean by _"how can I set the code in SQLiteConnetion object"_? Your code seems to be correct. Could you provide a code snippet on what is not working? – Botond Botos Nov 19 '16 at 10:14
  • @botond.botos hi Actually I am facing the problem is, please look this url : http://stackoverflow.com/questions/40689467/where-to-store-my-windows-applications-data . Then I thought create a default path when install my application on any local computer while creating setup file in wpf c# – lashja Nov 19 '16 at 10:20
  • There's a different approach described at http://stackoverflow.com/questions/15292880/create-sqlite-database-and-table. Instead of copying a database file it creates it on the fly. – Botond Botos Nov 19 '16 at 10:38

1 Answers1

2

I have this method to copy database:

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);
    }
}
Paviel Kraskoŭski
  • 1,429
  • 9
  • 16