System.Data.SQLite数据库简介
SQLite介绍 在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义、表、索引和数据本身)都保存在一个单一的文件中。并且,SQLite是一个用C实现的类库,它在内存消耗、文件体
SQLite介绍
在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义、表、索引和数据本身)都保存在一个单一的文件中。并且,SQLite是一个用C实现的类库,它在内存消耗、文件体积、简单性方面都有不错的表现,如果数据在10W条以下,查询速度也是相当快的。
SQLite具有以下特征:
实现多数SQL92的标准,包括事务(原子性、一致性、隔离性和持久性)、触发器和大多数的复杂查询。
不对插入或者更新的数据进行类型检查,你可以将字符串插入到整数列中(这个可能让有些用户不太适应)。
支持Windows/Linux/Unix等主流系统,还支持嵌入式系统如Android或Windows Mobile。
System.Data.SQLite介绍
System.Data.SQLite是SQLite的加强版,它可以无需.NET Framework支持,由于它内部包含了一个ADO.NET 2.0引擎,所以.NET开发人员可以利用System.Data.SQLite方便地开发.NET程序。
System.Data.SQLite及SQLite也有一些限制,比如不支持行级及表级锁,当一个连接锁定数据库以用于写入数据,其它的数据库连接只能等待那个连接操作完成之后进行读写操作,SQLite.NET尝试在超时期内多次尝试。
实际上对于大型的应用我们都会选择一些大型专业的数据库,System.Data.SQLite和SQLite适合于一些受限的场合,比如手机等。在这里我讲一个真实的经历,在此前我曾经做过一个小型系统,要分析三个Excel文件,其中两个的记录大约在400条左右,而另外一个大约是1万条左右,对于这么一个系统如果使用数据库,即使单机版的Access,导入之后利用数据库的特性进行分析,将是一个相对较为简单的事情,因为我们可以在数据库里使用连接查询,还可以对记录使用数据库函数,但是对方提供的信息是部署的机器上尽管安装了Office,但是只是安装了Word、Excel和Outlook,而没有Access,对方也不希望安装其它的软件,由于我也不能确定没有安装Access的机器上是否能通过OleDB访问.mdb文件,所以没有办法,只有才有内存表的形式,即将Excel中的数据读取到DataTable中,然后对三个DataTable进行分析,尽管做了很多优化,但是效率仍然不是太理想。对于这种情况,如果我当时知道System.Data.SQLite就好办多了,将三个Excel中的数据导入到System.Data.SQLite中,然后利用System.Data.SQLite提供的函数处理起来是相当省事和方便的。对于System.Data.SQLite来说,部署时不需要安装,仅需要一个System.Data.SQLite.dll就够了,这个dll仅866K!而且它不需要像使用Com组件那样需要注册。
在VS2008中操作System.Data.SQLite
为了方便开发者,System.Data.SQLite提供了VS2005和VS2008的支持,甚至还支持.NET 3.5 SP1中的Entity Framework,下面是在VS2008中使用System.Data.SQLite设计器的情况:
首先打开VS2008中的服务器资源管理器,如下图:
接着在数据连接上点击鼠标右键,如下图所示:
然后选择“添加连接”,如下图所示:
这时候选择System.Data.SQLite使用的数据库文件,文件后缀默认是.db3,还可以点击下方的“测试连接”按钮,如果没有问题就会弹出正确的对话框,点击“确定”按钮之后在服务器资源管理器中就会出现如下的情况:
这样我们就可以像操作SQL Server中的库一样操作System.Data.SQLite中的表了。
System.Data.SQLite数据库通用类
针对对数据库的操作情况,分为以下几种情况:
创建数据库文件;
返回DataTable;
返回DataReader;
执行增删改,返回受影响的行数;
执行查询,返回第一行第一列(通常用于带有行函数的查询,如SUM/AVG/COUNT等);
返回库中所有的表;
因为在System.Data.SQLite中不存在存储过程,所以所有的操作都是基于文本的SQL语句,为了避免SQL注入,所以使用了参数化的SQL语句,这个数据库通用类如下:
System.Data.SQLite数据库通用类的用法
下面演示一下刚刚编写的数据库通用类的用法,代码如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
