见过不少人、经过不少事、也吃过不少苦,感悟世事无常、人心多变,靠着回忆将往事串珠成链,聊聊感情、谈谈发展,我慢慢写、你一点一点看......

1.安装并引用System.Data.SQLite

通过NuGet包管理器安装,Install-Package System.Data.SQLite

2.创建数据库

string dbFilename =  "test.db";if (!File.Exists(dbFilename)){    SQLiteConnection.CreateFile(dbFilename);}

3.设置数据库密码

string connectionString = string.Format("Data Source={0};Version=3;",dbFilename);using (SQLiteConnection connection = new SQLiteConnection(connectionString)){    connection.Open();//打开数据库    connection.ChangePassword("123456");//设置密码}

4.连接数据库

string connectionString =string.Format("Data Source={0}; Version=3; Password={1};",dbFilename,"123456");using (SQLiteConnection connection = new SQLiteConnection(connectionString)){    connection.Open();}

5.创建表

using (SQLiteConnection connection = new SQLiteConnection(connectionString)){    connection.Open();    string commandText = "CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR(100), Code VARCHAR(100),Password VARCHAR(100))";    using (SQLiteCommand command = new SQLiteCommand(commandText, connection))    {        command.ExecuteNonQuery();//执行sql    }}

6.添加数据

using (SQLiteConnection connection = new SQLiteConnection(connectionString)){    connection.Open();    string commandText = "insert into Users (Name, Code,Password) values (@name, @code,@password)";    using (SQLiteCommand command = new SQLiteCommand(commandText, connection))    {        // 设置参数值        command.Parameters.AddWithValue("@name", "管理员");        command.Parameters.AddWithValue("@code", "admin");        command.Parameters.AddWithValue("@password", "123456");        // 执行语句        command.ExecuteNonQuery();    }}

7.修改数据

using (SQLiteConnection connection = new SQLiteConnection(connectionString)){    connection.Open();    string commandText = "update Users SET Password=@password WHERE Code = @code";    using (SQLiteCommand command = new SQLiteCommand(commandText, connection))    {        // 设置参数值        command.Parameters.AddWithValue("@code", "admin");        command.Parameters.AddWithValue("@password", "admin123456");        // 执行语句        command.ExecuteNonQuery();    }}

8.查询数据

using (SQLiteConnection connection = new SQLiteConnection(connectionString)){    connection.Open();    string commandText  = "select * from Users";    using (SQLiteCommand command = new SQLiteCommand(commandText, connection))    {        using (SQLiteDataReader reader = command.ExecuteReader())        {            while (reader.Read())            {                Console.WriteLine($"ID: {reader["Id"]}, 名称: {reader["Name"]}, 编码: {reader["Code"]}");            }        }    }}

9.删除数据

using (SQLiteConnection connection = new SQLiteConnection(connectionString)){    connection.Open();    string commandText = "delete from  Users where Code = @code";    using (SQLiteCommand command = new SQLiteCommand(sql, connection))    {        // 设置参数值        command.Parameters.AddWithValue("@code", "admin");        // 执行语句        command.ExecuteNonQuery();    }}

关注我,不失联。有啥问题请留言。

感情恋爱合集

职业发展故事

常用代码片段

程序开发教程

自我备考经验 

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐