using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;
namespace OTSExtremum.Data
{
///
/// SQLite 操作类
///
public class SqlHelper
{
///
/// 数据库连接字符串
///
public static string connectionString = "";
///
/// 数据库连接定义
///
private SQLiteConnection dbConnection;
///
/// SQL命令定义
///
private SQLiteCommand dbCommand;
private SQLiteDataAdapter dataAdapter;
///
/// 数据读取定义
///
private SQLiteDataReader dataReader;
private DataTable dt;
///
/// 构造函数
///
/// 连接SQLite库字符串
public SqlHelper(string con)
{
try
{
dbConnection = new SQLiteConnection(con);
dbConnection.Open();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//Log(e.ToString());
}
}
///
/// 构造函数
///
/// 连接SQLite库字符串
public SqlHelper()
{
try
{
dbConnection = new SQLiteConnection(connectionString);
dbConnection.Open();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
///
/// 创建一个指定名字的数据库
///
///
public void CreateNewDatabase(string basename)
{
try
{
SQLiteConnection.CreateFile(basename);
}
catch (Exception)
{
}
}
///
/// 删除指定的数据库文件
///
///
public void DeleteDataBase(string basename)
{
try
{
if (System.IO.File.Exists(basename))
{
System.IO.File.Delete(basename);
}
}
catch (Exception)
{
}
}
///
/// 执行SQL命令
///
/// The query.
/// SQL命令字符串
public DataTable ExecuteQuery(string queryString)
{
try
{
dataAdapter = new SQLiteDataAdapter(queryString, dbConnection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dt = ds.Tables[0];
}
catch (Exception)
{
//Log(e.Message);
}
return dt;
}
///
/// 关闭数据库连接
///
public void CloseConnection()
{
//销毁Commend
if (dbCommand != null)
{
dbCommand.Cancel();
}
dbCommand = null;
//销毁Reader
if (dataReader != null)
{
dataReader.Close();
}
dataReader = null;
//销毁Connection
if (dbConnection != null)
{
dbConnection.Close();
}
dbConnection = null;
}
///
/// 读取整张数据表
///
/// The full table.
/// 数据表名称
public DataTable ReadFullTable(string tableName)
{
string queryString = "SELECT * FROM " + tableName;
return ExecuteQuery(queryString);
}
///
/// /// 执行一个查询语句,返回一个包含查询结果的DataTable
/// ///
/// /// 要执行的查询语句
/// /// 执行查询语句的参数
/// ///
public DataTable ExecuteDataTable(string sql, params SQLiteParameter[] parameters)
{
using (SQLiteCommand command = new SQLiteCommand(sql, dbConnection))
{
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data);
return data;
}
}
///
/// /// tbale转list
/// ///
/// ///
/// ///
public List TableToList(DataTable table) where T : class, new()
{
var result = new List();
var propertys = typeof(T).GetProperties();
foreach (DataRow dr in table.Rows)
{
var item = new T();
result.Add(item);
foreach (var pi in propertys)
{
if (!table.Columns.Contains(pi.Name))
continue;
var value = dr[pi.Name];
if (value is DBNull || value == null)
continue;
if (value.GetType().ToString() == "System.Int64")
{
pi.SetValue(item, Convert.ToInt32(value));
}
else
{
pi.SetValue(item, value);
}
}
}
return result;
}
///
/// /// tbale转list
/// ///
/// ///
/// ///
public List RowsToList(DataRow[] table) where T : class, new()
{
var result = new List();
var propertys = typeof(T).GetProperties();
foreach (DataRow dr in table)
{
var item = new T();
result.Add(item);
foreach (var pi in propertys)
{
if (!dr.Table.Columns.Contains(pi.Name))
continue;
var value = dr[pi.Name];
if (value is DBNull || value == null)
continue;
if (value.GetType().ToString() == "System.Int64")
{
pi.SetValue(item, Convert.ToInt32(value));
}
else
{
pi.SetValue(item, value);
}
}
}
return result;
}
///
/// 向指定数据表中插入数据
///
/// The values.
/// 数据表名称
/// 插入的数值
public DataTable InsertValues(string tableName, string[] values)
{
//获取数据表中字段数目
int fieldCount = ReadFullTable(tableName).Columns.Count;
//当插入的数据长度不等于字段数目时引发异常
if (values.Length != fieldCount)
{
throw new SQLiteException("values.Length!=fieldCount");
}
string queryString = "INSERT INTO " + tableName + " VALUES (" + "'" + values[0] + "'";
for (int i = 1; i < values.Length; i++)
{
queryString += ", " + "'" + values[i] + "'";
}
queryString += " )";
return ExecuteQuery(queryString);
}
///
/// 更新指定数据表内的数据
///
/// The values.
/// 数据表名称
/// 字段名
/// 字段名对应的数据
/// 关键字
/// 关键字对应的值
/// 运算符:=,<,>,...,默认“=”
public DataTable UpdateValues(string tableName, string[] colNames, string[] colValues, string key, string value, string operation = "=")
{
//当字段名称和字段数值不对应时引发异常
if (colNames.Length != colValues.Length)
{
throw new SQLiteException("colNames.Length!=colValues.Length");
}
string queryString = "UPDATE " + tableName + " SET " + colNames[0] + "=" + "'" + colValues[0] + "'";
for (int i = 1; i < colValues.Length; i++)
{
queryString += ", " + colNames[i] + "=" + "'" + colValues[i] + "'";
}
queryString += " WHERE " + key + operation + "'" + value + "'";
return ExecuteQuery(queryString);
}
///
/// 删除指定数据表内的数据
///
/// The values.
/// 数据表名称
/// 字段名
/// 字段名对应的数据
public DataTable DeleteValuesOR(string tableName, string[] colNames, string[] colValues, string[] operations)
{
//当字段名称和字段数值不对应时引发异常
if (colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length)
{
throw new SQLiteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length");
}
string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + "'" + colValues[0] + "'";
for (int i = 1; i < colValues.Length; i++)
{
queryString += "OR " + colNames[i] + operations[0] + "'" + colValues[i] + "'";
}
return ExecuteQuery(queryString);
}
///
/// 删除指定数据表内的数据
///
/// The values.
/// 数据表名称
/// 字段名
/// 字段名对应的数据
public DataTable DeleteValuesAND(string tableName, string[] colNames, string[] colValues, string[] operations)
{
//当字段名称和字段数值不对应时引发异常
if (colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length)
{
throw new SQLiteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length");
}
string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + "'" + colValues[0] + "'";
for (int i = 1; i < colValues.Length; i++)
{
queryString += " AND " + colNames[i] + operations[i] + "'" + colValues[i] + "'";
}
return ExecuteQuery(queryString);
}
///
/// 创建数据表
/// +
/// The table.
/// 数据表名
/// 字段名
/// 字段名类型
public DataTable CreateTable(string tableName, string[] colNames, string[] colTypes)
{
string queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "( " + colNames[0] + " " + colTypes[0];
for (int i = 1; i < colNames.Length; i++)
{
queryString += ", " + colNames[i] + " " + colTypes[i];
}
queryString += " ) ";
return ExecuteQuery(queryString);
}
///
/// 删除数据库表
///
///
///
public DataTable DeleteTable(string tableName)
{
string queryString = "drop table " + tableName;
return ExecuteQuery(queryString);
}
///
/// Reads the table.
///
/// The table.
/// Table name.
/// Items.
/// Col names.
/// Operations.
/// Col values.
public DataTable ReadTable(string tableName, string[] items, string[] colNames, string[] operations, string[] colValues)
{
string queryString = "SELECT " + items[0];
for (int i = 1; i < items.Length; i++)
{
queryString += ", " + items[i];
}
queryString += " FROM " + tableName + " where 1=1";
//+ " WHERE " + colNames[0] + " " + operations[0] + " " + colValues[0];
for (int i = 0; i < colNames.Length; i++)
{
queryString += " AND " + colNames[i] + " " + operations[i] + " " + colValues[0] + " ";
}
return ExecuteQuery(queryString);
}
}
}