C# 与 SQLite 的使用

C# SourceCode Example

C# 源码案例

This Example shows the basic things when working with SQLite.NET.

这个例子显示了使用SQLite.NET时的基本的事情。

Basically this is how all ADO.NET Data Providers work ;D

基本上,这是ADO.NET Data Providers 怎样工作的。

Before you can use Finisar's SQLite.NET Data Provider, you have to tell your IDE about it.

在你使用 Finisar's SQLite.NET Data Provider之前,你必须告诉你的IDE关于它的事情。

This is how it is done in "Microsoft Visual C# 2005 Express Edition Beta 2" - It should not differ much in other IDEs...

在"Microsoft Visual C# 2005 Express Edition Beta 2"中,它是这样做的 -  它与其他IDE没有不同。

1) Place the .dll files you downloaded from this website into your projects binary folder.

1)将你从网站下载的 System.Data.SQLite.dll 放到你项目目录的bin文件夹内。

2) Press 'Project -> Add Reference...' in the menu bar.

2)在菜单栏点击“项目->添加引用'

3) Select the 'Browse'-Tab and select the "SQLite.dll".

3)选择‘浏览’选择System.Data.SQLite.dll

4) Press 'Ok' and you are finishd ;D

4)点击'OK' 然后你就完成了。

C# SourceCode:

using Finisar.SQLite;
// [snip] - As C# is purely object-oriented the following lines must be put into a class:
// We use these three SQLite objects:
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
// open the connection:
10 sqlite_conn.Open();
11 // create a new SQL command:
12 sqlite_cmd = sqlite_conn.CreateCommand();
13 // Let the SQLiteCommand object know our SQL-Query:
14 sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(100));";
15 // Now lets execute the SQL ;D
16 sqlite_cmd.ExecuteNonQuery();
17 // Lets insert something into our new table:
18 sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";
19 // And execute this again ;D
20 sqlite_cmd.ExecuteNonQuery();
21 // ...and inserting another line:
22 sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";
23 // And execute this again ;D
24 sqlite_cmd.ExecuteNonQuery();
25 // But how do we read something out of our table ?
26 // First lets build a SQL-Query again:
27 sqlite_cmd.CommandText = "SELECT * FROM test";
28 // Now the SQLiteCommand object can give us a DataReader-Object:
29 sqlite_datareader=sqlite_cmd.ExecuteReader();
30 // The SQLiteDataReader allows us to run through the result lines:
31 while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
32 {
33 // Print out the content of the text field:
34 System.Console.WriteLine( sqlite_datareader["text"] );
35 }
36 // We are ready, now lets cleanup and close our connection:
37 sqlite_conn.Close();
38
posted @   Margin22  阅读(690)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示