1.下载 SQLite (下载网址: https://www.sqlite.org/download.html)
下载下图三个文件
2.创建数据库
- 解压 sqlite-tools-win32-x86-3310100.zip
- 运行 sqlite3.exe
- 输入 .open [dbName].db (打开或创建数据库)
- 按enter就会在当前目录创建数据库
3.安装 System.Data.SQLite.dll
- 打开 NuGet程序包管理器控制台
- 输入 Install-Package System.Data.SQLite,就会在选择的项目中安装dll
- 安装完成就已经将对应的dll添加到引用中了
4.可视化管理工具
- navicat 12 for sqlite 下载地址: http://www.ddooo.com/softdown/130289.htm#dltab
ps:挺喜欢的它的界面的,可惜在这里界面的数据库用System.Data.SQLite.dll连接不上,好像是对密码的加密方式不一样
- SQLite Developer 下载地址:https://www.onlinedown.net/soft/61654.htm
5.对sqlite的增删改查
using System.Data;
using System.Data.SQLite;
namespace WindowsFormsApp1
{
class SqliteAdo
{
string _connStr = "data source=";
// "data source=数据库文件地址;Password=123"
//有密码带上密码;没有不要带,会报错
public SqliteAdo(string dbPath, string password = null, int version = 3)
{
_connStr += dbPath;
_connStr += string.Format(";version={0}", version);
if (password != null)
{
_connStr += string.Format(";password={0}", password);
}
}
public SQLiteConnection Open()
{
SQLiteConnection dbConnection = new SQLiteConnection(_connStr);
dbConnection.Open(); //不存在对应的数据库会创建
return dbConnection;
}
/// <summary>
/// 修改密码
/// </summary>
public bool ChangePassword(string password)
{
using (SQLiteConnection dbConnection = Open())
{
dbConnection.ChangePassword(password);
dbConnection.Close();
}
return true;
}
/// <summary>
/// 查询
/// </summary>
public DataSet DataReader(string selectSql)
{
DataSet dataSet = new DataSet();
using (SQLiteConnection dbConnection = Open())
{
SQLiteDataAdapter sQLiteDataReader = new SQLiteDataAdapter(selectSql, dbConnection);
sQLiteDataReader.Fill(dataSet);
dbConnection.Close();
}
return dataSet;
}
/// <summary>
/// 增改删
/// </summary>
public int ExecQuery(string execSql)
{
int resultNum = -1; //受影响的行数
using (SQLiteConnection dbConnection = Open())
{
SQLiteCommand command = new SQLiteCommand(execSql, dbConnection);
resultNum = command.ExecuteNonQuery();
dbConnection.Close();
}
return resultNum;
}
}
}