读取SQLite数据库,就是读取一个路径\192.168.100.**positiondb.sqlite下的文件
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
这个放在</configuration>里面去
我在winform 里的代码
public void jiazaiweizhi()
{
string sql = "SELECT * FROM messages";
string connStr = @"Data Source=" + @"\192.168.100.**positiondb.sqlite;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, "数据库信息");
textBox1.Text = "当前位置" + Environment.NewLine + dt.Rows[0][1].ToString();
}
}
}
.Net SQLite数据库驱动以及System.Data.SQLite.dll下载最新地址:
http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
(电脑上可以搜索System.Data.SQLite.dll这个dll,加载进入项目之后 可以看它的版本)
SQLite管理工具下载地址:
http://www.cr173.com/soft/94247.html
App.config文件修改:
- <?xml version="1.0"?>
- <configuration>
- <appSettings>
- <!--FailIfMissing=false表示数据库不存在时就会自动创建-->
- <add key="DbSQLite" value=https://www.isolves.com/it/cxkf/yy/C/2021-12-16/"data source=|DataDirectory|DB.db3;Pooling=true;FailIfMissing=false"/>
- </appSettings>
- </configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
这个放在</configuration>里面去
数据库读写助手SqliteHelper.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Data.Common;
- using System.Data.SQLite;
- public class SqliteHelper
- {
- public SqliteHelper()
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- }
- private static SQLiteConnection GetConnection()
- {
- string connStr = System.Configuration.ConfigurationManager.AppSettings["DbSQLite"].ToString();
- SQLiteConnection conn = new SQLiteConnection(connStr);
- conn.Open();
- return conn;
- }
- public static int ExecuteSql(string sql)
- {
- using (SQLiteConnection conn = GetConnection())
- {
- var cmd = new SQLiteCommand(sql, conn);
- return cmd.ExecuteNonQuery();
- }
- }
- public static int ExecuteScalar(string sql)
- {
- using (SQLiteConnection conn = GetConnection())
- {
- var cmd = new SQLiteCommand(sql, conn);
- object o = cmd.ExecuteScalar();
- return int.Parse(o.ToString());
- }
- }
- public static SQLiteDataReader ExecuteReader(string sql)
- {
- SQLiteConnection conn = GetConnection();
- var cmd = new SQLiteCommand(sql, conn);
- SQLiteDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- return myReader;
- }
- public static DataSet ExecDataSet(string sql)
- {
- using (SQLiteConnection conn = GetConnection())
- {
- var cmd = new SQLiteCommand(sql, conn);
- SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- return ds;
- }
- }
- }
-
- //判断表是否存在,不存在则生成
- int result = SqliteHelper.ExecuteScalar("SELECT COUNT(*) FROM sqlite_master where type='table' and name='tb'");
推荐阅读
- Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?
- Win下部署多个MySQL数据库实例
- 数据库迁移有什么技巧?|分享强大的database迁移和同步工具
- 数据库,MySQL,实战,优化,多表联合查询排序问题优化
- SpringBoot通过JdbcTemplate操作MySQL数据库
- 数据库:评估安全风险4个要点
- 读写文件 Python csv模块
- mysqlcheck 修复数据库表
- MySQL MyCAT 读写分离实战
- 为什么微服务要有独立数据库