0

I have a problem that when i want to create database and specify its name it is created in the specified directory and in directory where the aplication is running. Why is that happening?

Code that creates database:

using System.Data.SQLite;
...

private static string AddDb(string dbName, string dbPassword)
{
    try
    {
        //default paths
        string startupPath = Environment.CurrentDirectory;
        string dataBasePath = startupPath + "\\DB\\" + dbName;
        
        //creating the dbfile
        SQLiteConnection.CreateFile(dataBasePath);
        
        //Opening connection
        SQLiteConnection dbConnString;
        dbConnString = new SQLiteConnection("Data Source =" + dbName + ";Version=3;");
        dbConnString.Open();
        dbConnString.ChangePassword(dbPassword);
        dbConnString.Close();
        
        return dataBasePath;
    }
    catch
    {
        MessageBox.Show("Failed to create database", "DB Creator");
        return "";
    }
}
Diego Montania
  • 322
  • 5
  • 12
phoenix
  • 1
  • 1

2 Answers2

1

The problem seems to be that you use different paths in CreateFile and your connection string.

If you look at your code below you'll notice that in one case you use a full path to create the file (databaseBasePath), while in the other case you only use the database file name in your connection string (dbName). Without an absolute path, this may be a different folder!

string dataBasePath = startupPath + "\\DB\\" + dbName;
SQLiteConnection.CreateFile(dataBasePath);
SQLiteConnection dbConnString;
dbConnString = new SQLiteConnection("Data Source =" + dbName + ";Version=3;");

It seems like Open then creates the file if it can't find it.

A word on the paths:

  1. You're not allowed to write the Program Files folder, so using the current folder for the database file is really a bad idea.
  2. The use of Environment.CurrentDirectory is also a bad idea. Depending on how you start your application, this may or may not be the folder that you think it is (see my comments to the other answers and this).
  3. Never assume that \ is actually the path separator. Use Path.Combine instead.

I suggest that you use Environment.GetFolderPath to get a location that's shared among all users (if the database content should be shared) or private to the current user (if all users should have their own database) and create the database there:

string baseFolder = Environment.GetFolderPath(<wherever it should be>);
string dataBasePath = Path.Combine(baseFolder, "DB", dbName);
SQLiteConnection.CreateFile(dataBasePath);
SQLiteConnection dbConnString = new SQLiteConnection(String.Format("Data Source = {0};Version=3;", dataBasePath);
Community
  • 1
  • 1
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
-1

The Environment.CurrentDirectory contains the directory that the application starts from by default. You can set this property. See the MSDN article Environment.CurrentDirectory Property

Wrongway
  • 47
  • 6
  • Wrong as well. It does not always contain the directory the application starts from, but the current working directory, which is something very different. See http://stackoverflow.com/questions/15653921/get-current-folder-path – Thorsten Dittmar Mar 30 '16 at 16:50
  • Yes it does. If you create a simple application to print it out, it outputs the directory the app starts in. He is using the Environment class not the Directory class. You can also change the Environment.CurrentDirectory. At least it does with Framework 4.5.2 and 4.6.1. Could he have changed it without realizing it? – Wrongway Mar 30 '16 at 17:24
  • If you create a shortcut to your simple app and specify a different working directory it does not. The docs also talk about the *working* directory. Be it as it may, applications installed to the Program Files folder will not be allowed to write there anyway, so the OP should use a proper location anyway. – Thorsten Dittmar Mar 30 '16 at 17:56