**
安装数据库包
**
在使用 SQLite 数据库时,你需要安装适当的 NuGet 包来提供与 SQLite 的集成。
1.打开 Visual Studio 中的你的项目
2.在顶部菜单栏中选择 “项目” -> “管理 NuGet 包”
3.在 NuGet 管理器中搜索 “System.Data.SQLite”
4.找到适合你项目的 SQLite 包,并点击安装
数据库类方法
//头文件
using Microsoft.Data.Sqlite;
using System.Data.SQLite;public static class MySqLite//数据库类{static string DbPath = @"D:\C#_program\北理测试1\北理测试1\北理测试1\北理测试\PowerField\bin\data";public static void ExecuteNonQuery(string sql, string dbName = "PowerField.db"){using (var connection = CreateDatabaseConnection(dbName)){connection.Open();using (var transaction = connection.BeginTransaction()){try{using (var command = connection.CreateCommand()){command.CommandText = sql;command.ExecuteNonQuery();}transaction.Commit();}catch (Exception ex){Console.WriteLine("Error executing SQL: " + ex.Message);transaction.Rollback();}}}}public static void ExecuteQuery(string sql, string dbName = "PowerField.db"){Console.WriteLine("1111111111");using (var connection = CreateDatabaseConnection(dbName)){connection.Open();using (var transaction = connection.BeginTransaction()){try{using (var command = connection.CreateCommand()){command.CommandText = sql;using (var reader = command.ExecuteReader()){//数据操作}}transaction.Commit();}catch (Exception ex){Console.WriteLine("Error executing SQL: " + ex.Message);transaction.Rollback();}}}}public static SQLiteConnection CreateDatabaseConnection(string dbName){if (!string.IsNullOrEmpty(DbPath) && !Directory.Exists(DbPath))Directory.CreateDirectory(DbPath);var dbFilePath = System.IO.Path.Combine(DbPath, dbName);return new SQLiteConnection("DataSource = " + dbFilePath);}//改public static void UpdateData(string name, string can1, string can2, string can3, string can4, string can5, string dbName = "PowerField.db")//改{string sql = "UPDATE weili_canshu SET can1 = @can1,can2 = @can2,can3 = @can3,can4 = @can4,can5 = @can5 WHERE name = @name";// 创建数据库连接using (var connection = CreateDatabaseConnection(dbName)){connection.Open(); // 打开数据库连接// 开始事务using (var transaction = connection.BeginTransaction()){try{// 创建命令对象using (var command = connection.CreateCommand()){command.CommandText = sql;// 添加参数command.Parameters.AddWithValue("@name", name);command.Parameters.AddWithValue("@can1", can1);command.Parameters.AddWithValue("@can2", can2);command.Parameters.AddWithValue("@can3", can3);command.Parameters.AddWithValue("@can4", can4);command.Parameters.AddWithValue("@can5", can5);Console.WriteLine("SQL 语句: " + command.CommandText);// 执行命令int rowsAffected = command.ExecuteNonQuery();// 提交事务transaction.Commit();Console.WriteLine($"成功更新 {rowsAffected} 行数据。");}}catch (Exception ex){Console.WriteLine("执行 SQL 出错: " + ex.Message);// transaction.Rollback(); // 发生异常时回滚事务}}}}//增public static void InsertData(string name, string c1,string c2, string c3, string c4, string c5,string id, string dbName = "PowerField.db"){string sql = "INSERT INTO weili_canshu (name,can1,can2,can3,can4,can5,id) VALUES (@name, @can1, @can2, @can3, @can4, @can5, @id)";// 创建数据库连接using (var connection = CreateDatabaseConnection(dbName)){connection.Open(); // 打开数据库连接// 开始事务using (var transaction = connection.BeginTransaction()){try{// 创建命令对象using (var command = connection.CreateCommand()){command.CommandText = sql;// 添加参数command.Parameters.AddWithValue("@name", name);command.Parameters.AddWithValue("@can1", c1);command.Parameters.AddWithValue("@can2", c2);command.Parameters.AddWithValue("@can3", c3);command.Parameters.AddWithValue("@can4", c4);command.Parameters.AddWithValue("@can5", c5);command.Parameters.AddWithValue("@id", id);// 执行命令int rowsAffected = command.ExecuteNonQuery();// 提交事务transaction.Commit();Console.WriteLine($"成功插入 {rowsAffected} 行数据。");}}catch (Exception ex){Console.WriteLine("执行 SQL 出错: " + ex.Message);transaction.Rollback(); // 发生异常时回滚事务}}}}
//删public static void DeleteData(string x){// string x = DamangeAssessmentWarhead.Text;// 创建数据库连接var connection = MySqLite.CreateDatabaseConnection("PowerField.db");connection.Open();// 开始事务SQLiteTransaction transaction = connection.BeginTransaction();try{// 创建命令对象SQLiteCommand command = connection.CreateCommand();command.CommandText = "DELETE FROM weili_canshu WHERE name ='" + x + "'";//当name=x时删除该条数据// 执行命令并获取受影响的行数int rowsAffected = command.ExecuteNonQuery();// 提交事务transaction.Commit();}catch (Exception ex){// 如果出现异常,回滚事务transaction.Rollback();Console.WriteLine("错误: " + ex.Message);}finally{// 关闭连接connection.Close();MessageBox.Show("删除成功!");}// DamangeAssessmentWarhead.Items.Remove(x);}}//调用ExecuteQuery(查)与ExecuteNonQuery(增删改)//例如 MySqLite.ExecuteQuery("SELECT * FROM weili_canshu");//直接写mysql查询语句//也可以自定义函数,调用方便 DeleteData();InsertData();等方法
一般查询数据库都要进行其他操作,所以查询一般写在类方法外面
//数据库——查var connection = MySqLite.CreateDatabaseConnection("PowerField.db");connection.Open();var command = connection.CreateCommand();command.CommandText = "SELECT * FROM weili_canshu";var reader = command.ExecuteReader();while (reader.Read())//read既是所读的一条数据,reader.GetString(0)代表数据的第一个值{
int id= reader.GetInt32(0);string name = reader.GetString(1);int age= reader.GetInt32(2);//id name age 数据读到了Console.WriteLine(name);//数据已得到,可以根据自己情况进行数据处理Console.WriteLine(age.toString());}connection.Close();