数据链接
/// <summary>
/// 获取当前数据链接
/// </summary>
/// <returns></returns>
public SQLiteConnection GetOpenConn()
{
string DBFile = FilePath;//设定数据库路径
SQLiteConnection conn = null;
if (File.Exists(DBFile))
{
string ConnStr = $"Data Source={DBFile};Version=3 ;BinaryGUID=False;";//将GUID保存为Text方式
conn = new SQLiteConnection(ConnStr);
try
{
conn.Open();
}
catch (Exception ex)
{
conn = null;
throw new Exception("数据库文件格式出错!");
}
}
else
{
conn = null;
}
return conn;
}
数据查询两个方法
1,获取数据表
string sql = $"select * from {TableName} where 1=1 {Where} ";
//SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
//cmdCreateTable.ExecuteNonQuery();
SQLiteDataAdapter mAdapter = new SQLiteDataAdapter(sql, conn);
DataTable dt = new DataTable();
mAdapter.Fill(dt);
if (dt != null)
{
IList<T> list = ConvertToModel<T>(dt);
return list;
}
else
{
return null;
}
2 一条一条数据读取
List<string> TableNames = new List<string>();
string sql = "select name from sqlite_master order by type";
SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
SQLiteDataReader reader = cmdCreateTable.ExecuteReader();
while (reader.Read())
{
string OneName = reader["Name"].ToString();
TableNames.Add(OneName);
}
reader.Close();
更新与插入以及删除操作
string sql = $"select * from {TableName} where 1=1 {Where} ";
SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
cmdCreateTable.ExecuteNonQuery();
SQLite如果有密码操作为:(似乎要钱买)
string ConnStr= "Data Source=F:/sqlitedb/test.db; Password=123";