DB Browser SQLite 操作类
程序员文章站
2022-06-10 18:10:19
...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data;
using System.Reflection;
using System.Diagnostics;
namespace DB_Browser_SQLite
{
class SqliteManager
{
public SQLiteConnection Connection = null;
protected bool isConnected
{
get { return _isConnected; }
}
private bool _isConnected = false;
private object _obj = new object();
protected virtual bool ConnectDatabase(String connectionString)
{
try
{
if (this.Connection != null)
{
this.Connection.Close();
this.Connection.Dispose();
this.Connection = null;
}
this.Connection = new SQLiteConnection(connectionString);
this.Connection.Open();
if (this.Connection.State == System.Data.ConnectionState.Open)
_isConnected = true;
else
_isConnected = false;
return _isConnected;
}
catch
{
Debug.WriteLine("Display Error Message.. ConnectSQLite");
return false;
}
}
protected int ExecuteNonQuery(String query)
{
if (_isConnected == false)
return -1;
try
{
SQLiteCommand cmd = null;
cmd = new SQLiteCommand(query, this.Connection);
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
}
/// <summary>
/// Database插入 Binary数据,图片和文件使用Insert
/// </summary>
/// <param name="query"> "@Image1"</param>
/// <param name="parameters">byte[] ...</param>
protected void ExecuteNonQuery(String query, params object[] parameters)
{
if (_isConnected == false)
return;
try
{
SQLiteCommand cmd = null;
cmd = new SQLiteCommand(query, this.Connection);
for (int i = 0; i < parameters.Length; i += 2)
{
SQLiteParameter parameter = new SQLiteParameter(parameters[i].ToString(), parameters[i + 1]);
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
}
catch
{
return;
}
}
protected SQLiteDataReader ExecuteReader(String query)
{
if (_isConnected == false)
return null;
try
{
SQLiteCommand cmd = null;
cmd = new SQLiteCommand(query, this.Connection);
SQLiteDataReader reader = cmd.ExecuteReader();
return reader;
}
catch
{
return null;
}
}
protected DataSet ExecuteAdpater(String query, DataSet ds)
{
if (_isConnected == false)
return null;
try
{
SQLiteDataAdapter adapter = new SQLiteDataAdapter(query, this.Connection);
adapter.Fill(ds);
return ds;
}
catch
{
return null;
}
}
protected IList<T> Map<T>(SQLiteDataReader reader) where T : class, new()
{
IList<T> collection = new List<T>();
while (reader.Read())
{
T obj = new T();
foreach (var property in obj.GetType().GetProperties()
.Where(p => p.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(DatabaseColumnAttribute)) != null).ToList())
{
try
{
var columnAttribute = property.GetCustomAttribute(typeof(DatabaseColumnAttribute)) as DatabaseColumnAttribute;
if (columnAttribute != null)
{
if (columnAttribute.Convert == true)
{
if (reader[property.Name] != DBNull.Value)
property.SetValue(obj, Convert.ChangeType(reader[property.Name], property.PropertyType));
}
else
{
if (reader.HasRows)
{
if (reader[property.Name] != DBNull.Value)
property.SetValue(obj, reader[property.Name]);
}
}
}
}
catch
{
}
}
collection.Add(obj);
}
return collection;
}
protected IList<T> ExecuteGet<T>(string cmdText) where T : class, new()
{
SQLiteCommand cmd = new SQLiteCommand(cmdText, this.Connection);
using (var reader = cmd.ExecuteReader())
{
return Map<T>(reader);
}
}
protected IList<T> ExecuteGet<T>(SQLiteDataReader reader) where T : class, new()
{
return Map<T>(reader);
}
}
public class DatabaseColumnAttribute : Attribute
{
public bool Convert { get; set; }
public bool IsPrimary { get; set; }
public bool IsIndex { get; set; }
public bool IsCondition { get; set; }
}
public class DatabaseTableAttribute : Attribute
{
public bool IsTable { get; set; }
}
}
上一篇: Android远程桌面助手之文件管理器
下一篇: oracle常用语句