You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

185 lines
4.8 KiB

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
namespace IOTContainer.SQLite
{
public class SQLiteAdapter : IDisposable
{
#region 构造函数
/// <summary>
/// 初始化适配器
/// </summary>
/// <param name="databasePath">数据库文件路径</param>
/// <param name="isReadOnly">是否只读</param>
public SQLiteAdapter(string databasePath, bool isReadOnly = false)
{
if (string.IsNullOrWhiteSpace(databasePath))
{
throw new Exception("Database File Cannot be Null Or White Space!");
}
this.DatabasePath = databasePath;
this.IsReadOnly = isReadOnly;
this._password = null;
this._connection = null;
}
/// <summary>
/// 初始化适配器
/// </summary>
/// <param name="databasePath">数据库文件路径</param>
/// <param name="password">数据库密码</param>
/// <param name="isReadOnly">是否只读</param>
public SQLiteAdapter(string databasePath, string password, bool isReadOnly = false)
{
if (string.IsNullOrWhiteSpace(databasePath))
{
throw new Exception("Database File Cannot be Null Or White Space!");
}
this.DatabasePath = databasePath;
this._password = password;
this.IsReadOnly = isReadOnly;
this._connection = null;
}
#endregion
#region 字段属性
/// <summary>
/// 密码
/// </summary>
private string _password;
/// <summary>
/// 数据库文件
/// </summary>
public string DatabasePath { get; private set; }
/// <summary>
/// 是否只读
/// </summary>
public bool IsReadOnly { get; }
private string DataSource
{
get
{
StringBuilder builder = new StringBuilder();
builder.Append(string.Format("Data Source={0}", this.DatabasePath));
if (!string.IsNullOrEmpty(_password))
{
builder.Append(string.Format(";Password={0}", this._password));
}
if (IsReadOnly)
{
builder.Append(string.Format(";Read Only={0}", this.IsReadOnly));
}
return builder.ToString();
}
}
/// <summary>
/// 连接
/// </summary>
private SQLiteConnection _connection;
/// <summary>
/// 获取是否连接
/// </summary>
public bool IsConnect { get; private set; }
/// <summary>
/// 获取是否销毁
/// </summary>
public bool IsDispose { get; private set; }
#endregion
#region 对外函数
/// <summary>
/// 连接数据库
/// </summary>
public void Connection()
{
try
{
if (IsDispose)
{
throw new Exception("Adapter Is Dispose!");
}
if (this.IsConnect)
{
throw new Exception("Adapter Is Aready Connect!");
}
this._connection = new SQLiteConnection(this.DataSource);
this._connection.Open();
this.IsConnect = true;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 获取执行器
/// </summary>
/// <returns>执行器</returns>
public SQLiteActuator GetActuator()
{
if (IsDispose)
{
throw new Exception("Adapter Is Dispose!");
}
if (!this.IsConnect)
{
throw new Exception("Adapter Is Not Connect!");
}
return new SQLiteActuator(new SQLiteCommand(this._connection));
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
if (!this.IsConnect)
{
throw new Exception("Adapter Is Not Connect!");
}
this.IsConnect = false;
this._connection.Close();
this._connection.Dispose();
this._connection = null;
}
/// <summary>
/// 销毁
/// </summary>
public void Dispose()
{
this.IsDispose = true;
if (this.IsConnect)
{
this.Close();
}
this._password = null;
this.DatabasePath = null;
}
#endregion
}
}