在C#中操作SQLite数据库,通常需要使用第三方库,如System.Data.SQLite,以下是C#操作SQLite的详细步骤和示例代码:
1、安装SQLite库
打开Visual Studio的“工具”菜单,选择“NuGet包管理器”->“管理解决方案的NuGet包”。
在搜索框中输入“System.Data.SQLite”,选择对应的包并点击“安装”。
2、创建数据库连接
使用SQLiteConnection
类来建立与SQLite数据库的连接。
示例代码:
using System.Data.SQLite; string connectionString = "Data Source=example.db;Version=3;"; // 指定数据库文件路径和版本 SQLiteConnection connection = new SQLiteConnection(connectionString); connection.Open();
3、创建数据表
使用SQLiteCommand
类执行SQL语句来创建数据表。
示例代码:
using System.Data.SQLite; string sqlCreateTable = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY, Name TEXT, Age INTEGER)"; SQLiteCommand command = new SQLiteCommand(sqlCreateTable, connection); command.ExecuteNonQuery();
4、插入数据
同样使用SQLiteCommand
类执行插入数据的SQL语句。
示例代码:
using System.Data.SQLite; string sqlInsert = "INSERT INTO Users (Name, Age) VALUES (@name, @age)"; SQLiteCommand insertCommand = new SQLiteCommand(sqlInsert, connection); insertCommand.Parameters.AddWithValue("@name", "John Doe"); insertCommand.Parameters.AddWithValue("@age", 30); insertCommand.ExecuteNonQuery();
5、查询数据
使用SQLiteCommand
类执行查询语句,并通过SQLiteDataReader
读取结果。
示例代码:
using System.Data.SQLite; string sqlSelect = "SELECT * FROM Users"; SQLiteCommand selectCommand = new SQLiteCommand(sqlSelect, connection); SQLiteDataReader reader = selectCommand.ExecuteReader(); while (reader.Read()) { int id = reader.GetInt32(0); string name = reader.GetString(1); int age = reader.GetInt32(2); Console.WriteLine($"Id: {id}, Name: {name}, Age: {age}"); } reader.Close();
6、更新数据
使用SQLiteCommand
类执行更新数据的SQL语句。
示例代码:
using System.Data.SQLite; string sqlUpdate = "UPDATE Users SET Age = @newAge WHERE Id = @id"; SQLiteCommand updateCommand = new SQLiteCommand(sqlUpdate, connection); updateCommand.Parameters.AddWithValue("@newAge", 35); updateCommand.Parameters.AddWithValue("@id", 1); updateCommand.ExecuteNonQuery();
7、删除数据
使用SQLiteCommand
类执行删除数据的SQL语句。
示例代码:
using System.Data.SQLite; string sqlDelete = "DELETE FROM Users WHERE Id = @id"; SQLiteCommand deleteCommand = new SQLiteCommand(sqlDelete, connection); deleteCommand.Parameters.AddWithValue("@id", 1); deleteCommand.ExecuteNonQuery();
8、关闭连接
操作完成后,记得关闭数据库连接。
示例代码:
connection.Close();
步骤 | 代码 | 说明 |
安装SQLite库 | 通过NuGet包管理器安装System.Data.SQLite | 确保项目中可以使用SQLite相关功能 |
创建数据库连接 | SQLiteConnection connection = new SQLiteConnection(connectionString); connection.Open(); | 建立与指定SQLite数据库文件的连接 |
创建数据表 | string sqlCreateTable = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY, Name TEXT, Age INTEGER)"; SQLiteCommand command = new SQLiteCommand(sqlCreateTable, connection); command.ExecuteNonQuery(); | 若数据表不存在则创建名为Users的数据表 |
插入数据 | string sqlInsert = "INSERT INTO Users (Name, Age) VALUES (@name, @age)"; SQLiteCommand insertCommand = new SQLiteCommand(sqlInsert, connection); insertCommand.Parameters.AddWithValue("@name", "John Doe"); insertCommand.Parameters.AddWithValue("@age", 30); insertCommand.ExecuteNonQuery(); | 向Users表中插入一条新记录 |
查询数据 | string sqlSelect = "SELECT * FROM Users"; SQLiteCommand selectCommand = new SQLiteCommand(sqlSelect, connection); SQLiteDataReader reader = selectCommand.ExecuteReader(); while (reader.Read()) { int id = reader.GetInt32(0); string name = reader.GetString(1); int age = reader.GetInt32(2); Console.WriteLine($"Id: {id}, Name: {name}, Age: {age}"); } reader.Close(); | 查询Users表中的所有记录并输出 |
更新数据 | string sqlUpdate = "UPDATE Users SET Age = @newAge WHERE Id = @id"; SQLiteCommand updateCommand = new SQLiteCommand(sqlUpdate, connection); updateCommand.Parameters.AddWithValue("@newAge", 35); updateCommand.Parameters.AddWithValue("@id", 1); updateCommand.ExecuteNonQuery(); | 将Id为1的用户的年龄更新为35 |
删除数据 | string sqlDelete = "DELETE FROM Users WHERE Id = @id"; SQLiteCommand deleteCommand = new SQLiteCommand(sqlDelete, connection); deleteCommand.Parameters.AddWithValue("@id", 1); deleteCommand.ExecuteNonQuery(); | 删除Id为1的用户记录 |
关闭连接 | connection.Close(); | 释放数据库连接资源 |
1、**问:如何在C#中使用参数化查询来防止SQL注入攻击?
答:在C#中使用SQLiteCommand
的Parameters
属性来添加参数化查询中的参数值,在插入数据时,不要直接将用户输入的值拼接到SQL语句中,而是使用command.Parameters.AddWithValue("@parameterName", parameterValue);
的方式来添加参数值,这样可以有效防止SQL注入攻击。
2、问:如果SQLite数据库文件不存在,程序会报错吗?
答:如果指定的SQLite数据库文件不存在,当尝试打开连接时,程序会抛出异常,在实际应用中,通常会先检查数据库文件是否存在,如果不存在则创建一个新的数据库文件,可以通过捕获异常或者提前进行文件存在性检查来处理这种情况。
C#操作SQLite数据库并不复杂,通过上述步骤和示例代码,相信大家可以轻松上手,在实际应用中,要注意合理使用参数化查询来保证数据库操作的安全性,同时要妥善处理可能出现的异常情况,确保程序的稳定性和可靠性,希望本文对大家有所帮助!