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

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
habipoguz
Posts: 9
Joined: Wed 20 Jan 2016 14:16

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

Post by habipoguz » Sat 30 Nov 2019 04:07

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?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Sat 30 Nov 2019 11:27

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.

habipoguz
Posts: 9
Joined: Wed 20 Jan 2016 14:16

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

Post by habipoguz » Sat 30 Nov 2019 21:28

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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Mon 02 Dec 2019 13:38

|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.

Post Reply