SQLite connection string format
Data Source=app.db
Examples
| Case | Connection string |
|---|---|
| A file next to the app | Data Source=app.db |
| Read-only | Data Source=app.db;Mode=ReadOnly |
| In-memory (one connection) | Data Source=:memory: |
| In-memory, shared between connections | Data Source=InMemorySample;Mode=Memory;Cache=Shared |
| Foreign keys on, command timeout 60 s | Data Source=app.db;Foreign Keys=True;Default Timeout=60 |
Every example on this page is checked with Microsoft.Data.Sqlite itself. Replace the passwords with your own, and keep real ones out of source control.
Where the file goes
A relative Data Source is resolved against the process's current directory, which differs between dotnet run, Visual Studio, IIS and a Windows service. Build an absolute path, for example with Path.Combine(AppContext.BaseDirectory, "app.db"), if the database must sit next to the app.
In-memory databases
Data Source=:memory: gives each connection its own empty database, which disappears when that connection closes. To share one in-memory database, give it a name, use Mode=Memory;Cache=Shared, and keep one connection open for as long as you need the data.
Where to keep it
Put it under ConnectionStrings in appsettings.json and read it with builder.Configuration.GetConnectionString("Default"). Keep passwords out of the file: user secrets in development, and an environment variable (ConnectionStrings__Default) or a secret store in production.
FAQ
Does SQLite need a server or a password?
No server. Password only works with an encryption-enabled SQLite build such as SQLCipher.
Why is my database empty in tests?
An in-memory database lives only while a connection is open. Keep one open, or use a named shared-cache database.
How do I use it with EF Core?
Add Microsoft.EntityFrameworkCore.Sqlite and call options.UseSqlite("Data Source=app.db").
Other databases
- PostgreSQL connection string
- SQL Server connection string
- MySQL connection string
- MongoDB connection string
- Redis connection string
- Connection string builder for every database