Search code examples
databasevb.netsqlitetemporary

what is the exact sytax to open sqlite "Temporary" database using vb.net


below will create in memory db

Dim cn As SQLiteConnection = New SQLiteConnection("Data Source=:memory:")

below will create disk file db with name abc in C:\Temp\

Dim cn As SQLiteConnection = New SQLiteConnection("Data Source=C:\Temp\abc")

or below will creates disk file db with name abc at default location ..\SqliteProject\bin\Debug

Dim cn As SQLiteConnection = New SQLiteConnection("Data Source=abc")

but.. how to create temporary db?? documentation Link1 Link2 Link3 Link4 says "to create temporary db use empty filename". but no one tells the exact code. I tried various combinations

Dim cn As SQLiteConnection = New SQLiteConnection("") 
Dim cn As SQLiteConnection = New SQLiteConnection("Data Source=") 
Dim cn As SQLiteConnection = New SQLiteConnection("Data Source=C:\Temp\")

but all throws exception/error


Solution

  • System.Data.SQLite uses the following code to check the database name:

      fileName = FindKey(opts, "Data Source", DefaultDataSource);
    
      if (String.IsNullOrEmpty(fileName))
      {
        fileName = FindKey(opts, "Uri", DefaultUri);
        if (String.IsNullOrEmpty(fileName))
        {
          fileName = FindKey(opts, "FullUri", DefaultFullUri);
          if (String.IsNullOrEmpty(fileName))
            throw new ArgumentException(UnsafeNativeMethods.StringFormat(CultureInfo.CurrentCulture, "Data Source cannot be empty.  Use {0} to open an in-memory database", MemoryFileName));
    

    So it is not possible to specfiy an empty name.

    However, it is possible to specify a URI filename with an empty path:

    New SQLiteConnection("FullUri=file:")