Page 1 of 1

Can another string be used in Data Source instead of DataDirectory?

Posted: Sat 30 Nov 2019 04:07
by habipoguz
Hi,
To avoid hard coding I am not using database path for Data Source. Instead of it, I am using |DataDirectory| and code in Form Loading like:

Code: Select all

AppDomain.CurrentDomain.SetData("DataDirectory", Settings.DataPath);
When I changed the database structure, I had to connect to the old database and upgrade the data to the new database. For this reason, I need to connect to the new database at the start of the program and the old database while clicking Browse button.
But when I use DataDirectory on both, I get an error. When I define another string instead of DataDirectory, for example, DataNewDirectory, in the model page like `<Connection Mode="ConnectionString" ConnectionString="Data Source=|DataNewDirectory|" Provider="Devart.Data.SQLite" />` I got "Illegal characters in path." error. How do I manage this?

Re: Can another string be used in Data Source instead of DataDirectory?

Posted: Sat 30 Nov 2019 11:27
by Shalex
Try using
ConnectionString="Data Source=|DataNewDirectory|\MyDatabase.db;"
instead of
ConnectionString="Data Source=|DataNewDirectory|"

If this doesn't help, localize the "Illegal characters in path." error in a small test project for reproducing and send it to us. Also, specify the full stack trace of the exception.

Re: Can another string be used in Data Source instead of DataDirectory?

Posted: Sat 30 Nov 2019 21:28
by habipoguz
Shalex, Thank you very much. There's no problem with that. In your example, at least the database file name has to be fixed. For example, C:\Users\Quince\Desktop\database.db, D:\Test\database.db, C:\Users\Quince\Documents\database.db ... But I have a textbox and a browse button. Therefore, the user can also select and use the database file from any location with any file name, for example from a flash disk. Moreover, these databases can have different names like I said. Even if nothing, the user may want to change the database file name so that he can enjoy it. In this case |DataNewDirectory|\MyDatabase.db will be helpless.

Re: Can another string be used in Data Source instead of DataDirectory?

Posted: Mon 02 Dec 2019 13:38
by Shalex
|DataDirectory| is the only macros name allowed by our provider.

Possible ways:

1. Assign path and file name chosen by your user:

Code: Select all

    var connection = new SQLiteConnection($"DataSource={ openFileDialog1.FileName }");
2. Or set your |DataDirectory| and open the first connection. After using Browse, change |DataDirectory| to the one chosen by your user and open the second connection.