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 = ??
3
  • 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? Commented Nov 19, 2016 at 10:14
  • @botond.botos hi Actually I am facing the problem is, please look this url : stackoverflow.com/questions/40689467/… . Then I thought create a default path when install my application on any local computer while creating setup file in wpf c# Commented Nov 19, 2016 at 10:20
  • There's a different approach described at stackoverflow.com/questions/15292880/…. Instead of copying a database file it creates it on the fly. Commented Nov 19, 2016 at 10:38

1 Answer 1

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);
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

@AbhilashJA So you should replace YourDatabase.db with test.s3db.
What are the benefits of using SQLiteConnection.BackupDatabase(...) over simple file copy?
@botond.botos Maybe that you don't have to write file copy code?
@PaviełKraskoŭski, I was asking because copying a file doesn't take more lines of code than creating a backup with SQLiteConnection API.
@botond.botos Maybe backup file has some additional information and somehow differs from a simple copy?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.