|
1 | 1 | --- |
2 | 2 | title: Use a SQLite database in a Windows app |
3 | 3 | description: Learn how to use a SQLite database in a Windows app to store and retrieve data in a lightweight database on the user's device. |
4 | | -ms.date: 08/01/2024 |
| 4 | +ms.date: 09/25/2025 |
5 | 5 | ms.topic: how-to |
6 | | -keywords: windows 10, windows 11, windows app sdk, SQLite, database |
| 6 | +keywords: windows 10, windows 11, windows app sdk, winui, SQLite, database |
7 | 7 | ms.localizationpriority: medium |
| 8 | +ms.custom: copilot-scenario-highlight |
8 | 9 | #customer intent: As a Windows developer, I want to learn how to use a SQLite database in a Windows app to store and retrieve data in a lightweight database on the user's device. |
9 | 10 | --- |
10 | 11 |
|
11 | 12 | # Use a SQLite database in a Windows app |
12 | 13 |
|
13 | | -You can use SQLite to store and retrieve data in a lightweight database on the user's device. This guide shows you how to do it in your Windows App SDK apps. |
| 14 | +[SQLite](https://sqlite.org/index.html) provides a reliable, lightweight database solution for storing data locally in Windows apps. Unlike traditional database systems that require separate server installations and complex configurations, SQLite runs entirely within your application process and stores data in a single file on the user's device. |
14 | 15 |
|
15 | | -## Some benefits of using SQLite for local storage |
| 16 | +This tutorial shows you how to integrate SQLite into your WinUI application using Microsoft's recommended data access libraries. You'll learn to set up a database, create tables, and implement basic data operations—all while following security best practices to protect against common vulnerabilities. |
| 17 | + |
| 18 | +## What you'll accomplish |
| 19 | + |
| 20 | +In this tutorial, you'll learn how to: |
| 21 | + |
| 22 | +- Configure your Windows app to use SQLite with the Microsoft.Data.SQLite library |
| 23 | +- Create and initialize a local database |
| 24 | +- Implement secure data insertion and retrieval methods |
| 25 | +- Build a simple user interface to interact with your data |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +To complete this tutorial, you need: |
| 30 | + |
| 31 | +- Visual Studio with Windows App SDK development workload |
| 32 | +- Basic familiarity with C# and XAML |
| 33 | +- Understanding of fundamental database concepts |
| 34 | + |
| 35 | +## Key improvements this approach offers |
| 36 | + |
| 37 | +Using SQLite for local data storage in your Windows app offers several advantages: |
| 38 | + |
| 39 | +- Simplified deployment: No separate database server installation required |
| 40 | +- Enhanced security: Data stays local on the user's device |
| 41 | +- Improved performance: Direct file access eliminates network latency |
| 42 | +- Reduced complexity: Single-file database simplifies backup and migration |
| 43 | + |
| 44 | +The techniques you'll learn apply to any Windows app that needs to store structured data locally, from simple settings storage to complex data management scenarios. |
| 45 | + |
| 46 | +> [!TIP] |
| 47 | +> You can use AI assistance to help [avoid SQL injection attacks in SQLite](#avoid-sql-injection-attacks). |
| 48 | +
|
| 49 | +## Benefits of SQLite for local storage |
16 | 50 |
|
17 | 51 | :heavy_check_mark: SQLite is lightweight and self-contained. It's a code library without any other dependencies. There's nothing to configure. |
18 | 52 |
|
@@ -40,7 +74,7 @@ The [Microsoft.Data.Sqlite](/dotnet/api/microsoft.data.sqlite) library implement |
40 | 74 |
|
41 | 75 | The rest of this guide helps you to use this library. |
42 | 76 |
|
43 | | -## Set up your solution to use the Microsoft.Data.SQlite library |
| 77 | +## Set up your solution to use the Microsoft.Data.SQLite library |
44 | 78 |
|
45 | 79 | We'll start with a basic Windows App SDK project, and then install the SQLite NuGet package. |
46 | 80 |
|
@@ -82,8 +116,8 @@ We'll do these things: |
82 | 116 |
|
83 | 117 | Open the `DataAccess` class in your project and make that class static. |
84 | 118 |
|
85 | | ->[!NOTE] |
86 | | ->While our example will place data access code in a static class, this is a design choice and is completely optional. |
| 119 | +> [!NOTE] |
| 120 | +> While the example will put your data access code in a static class, this is a design choice and is completely optional. |
87 | 121 |
|
88 | 122 | ```csharp |
89 | 123 | public static class DataAccess |
@@ -248,6 +282,18 @@ public MainWindow() |
248 | 282 |
|
249 | 283 | That's it. Explore the [Microsoft.Data.Sqlite](/dotnet/api/microsoft.data.sqlite) to see what other things you can do with your SQLite database. Check out the links below to learn about other ways to use data in your Windows apps. |
250 | 284 |
|
| 285 | +## Avoid SQL injection attacks |
| 286 | + |
| 287 | +The code in this example uses parameterized queries to prevent SQL injection attacks. Never concatenate user input into a SQL query string. Always use parameters. You can ask Copilot for more tips on avoiding SQL injection attacks. |
| 288 | + |
| 289 | +The following text shows an example prompt for Copilot: |
| 290 | + |
| 291 | +```copilot-prompt |
| 292 | +Can you provide some best practices to avoid SQL injection attacks when writing SQLite queries in C# code? |
| 293 | +``` |
| 294 | + |
| 295 | +Copilot is powered by AI, so surprises and mistakes are possible. For more information, see [Copilot FAQs](https://aka.ms/copilot-general-use-faqs). |
| 296 | + |
251 | 297 | ## Related content |
252 | 298 |
|
253 | 299 | ### Connect your app directly to a SQL Server database |
|
0 commit comments