参考网址: https://www.cnblogs.com/nsky/p/4479850.html
1、新建WinForm项目,创建Button
2、右键解决方案资源管理器——管理NuGet程序包——搜索sqlite,下载System.Data.SQLite(x86/x64)——下载后AppConfig内容为——
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<remove invariant="System.Data.SQLite" /><add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /></DbProviderFactories>
</system.data>
</configuration>
3、SQLite下载的平台类型不对,会造成报错:
未能加载文件或程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确的程序。
所以需要右键解决方案资源管理器 中项目——属性——生成——平台目标——更改为x64
4、在App.config中添加连接数据库
<connectionStrings>
<add name="sqlite" connectionString="Data Source=F:\SQLiteStudio\MyCustomDB.db\document.db;Pooling=true;FailIfMissing=false"
providerName="System.Data.SQLite" />
</connectionStrings>
5、WinForm中编写代码,查询数据库中数据信息
string sql = "SELECT * FROM 表名";
string connStr = @"Data Source=" + @"F:\SQLiteStudio\MyCustomDB.db;Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
using (SQLiteConnection conn = new SQLiteConnection(connStr))
{
using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
MessageBox.Show(dt.Columns[1].ColumnName, "数据库信息");
}
}