sqlite帮助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data; namespace SqliteDemo
{
/// <summary>
/// SQLite 操作类
/// </summary>
class SqLiteHelper
{ /// <summary>
/// 数据库连接定义
/// </summary>
private SQLiteConnection dbConnection; /// <summary>
/// SQL命令定义
/// </summary>
private SQLiteCommand dbCommand; /// <summary>
/// 数据读取定义
/// </summary>
private SQLiteDataReader dataReader; /// <summary>
/// 数据读取
/// </summary>
private SQLiteDataAdapter dataAdapter; /// <summary>
/// 构造函数
/// </summary>
/// <param name="connectionString">连接SQLite库字符串</param>
public SqLiteHelper(string connectionString)
{
try
{
dbConnection = new SQLiteConnection(connectionString);
dbConnection.Open();
}
catch (Exception e)
{
Log(e.ToString());
}
}
/// <summary>
/// 执行SQL命令
/// </summary>
/// <returns>The query.</returns>
/// <param name="queryString">SQL命令字符串</param>
public SQLiteDataReader ExecuteQuery(string queryString)
{
try
{
dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = queryString;
dataReader = dbCommand.ExecuteReader();
}
catch (Exception e)
{
Log(e.Message);
} return dataReader;
} public DataTable GetDataTable(string tableName)
{
string queryString = "SELECT * FROM " + tableName;
DataTable dt = new DataTable();
try
{
dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = queryString;
SQLiteDataAdapter ad = new SQLiteDataAdapter(dbCommand);
ad.Fill(dt);
}
catch (Exception e)
{
Log(e.Message);
}
return dt;
} /// <summary>
/// 关闭数据库连接
/// </summary>
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; } /// <summary>
/// 读取整张数据表
/// </summary>
/// <returns>The full table.</returns>
/// <param name="tableName">数据表名称</param>
public SQLiteDataReader ReadFullTable(string tableName)
{
string queryString = "SELECT * FROM " + tableName;
return ExecuteQuery(queryString);
} /// <summary>
/// 向指定数据表中插入数据
/// </summary>
/// <returns>The values.</returns>
/// <param name="tableName">数据表名称</param>
/// <param name="values">插入的数值</param>
public SQLiteDataReader InsertValues(string tableName, string[] values)
{
//获取数据表中字段数目
int fieldCount = ReadFullTable(tableName).FieldCount;
//当插入的数据长度不等于字段数目时引发异常
if (values.Length != fieldCount)
{
throw new SQLiteException("values.Length!=fieldCount");
} string queryString = "INSERT INTO " + tableName + " VALUES (" + "'" + values[] + "'";
for (int i = ; i < values.Length; i++)
{
queryString += ", " + "'" + values[i] + "'";
}
queryString += " )";
return ExecuteQuery(queryString);
} /// <summary>
/// 更新指定数据表内的数据
/// </summary>
/// <returns>The values.</returns>
/// <param name="tableName">数据表名称</param>
/// <param name="colNames">字段名</param>
/// <param name="colValues">字段名对应的数据</param>
/// <param name="key">关键字</param>
/// <param name="value">关键字对应的值</param>
/// <param name="operation">运算符:=,<,>,...,默认“=”</param>
public SQLiteDataReader 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[] + "=" + "'" + colValues[] + "'";
for (int i = ; i < colValues.Length; i++)
{
queryString += ", " + colNames[i] + "=" + "'" + colValues[i] + "'";
}
queryString += " WHERE " + key + operation + "'" + value + "'";
return ExecuteQuery(queryString);
} /// <summary>
/// 删除指定数据表内的数据
/// </summary>
/// <returns>The values.</returns>
/// <param name="tableName">数据表名称</param>
/// <param name="colNames">字段名</param>
/// <param name="colValues">字段名对应的数据</param>
public SQLiteDataReader 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[] + operations[] + "'" + colValues[] + "'";
for (int i = ; i < colValues.Length; i++)
{
queryString += "OR " + colNames[i] + operations[] + "'" + colValues[i] + "'";
}
return ExecuteQuery(queryString);
} /// <summary>
/// 删除指定数据表内的数据
/// </summary>
/// <returns>The values.</returns>
/// <param name="tableName">数据表名称</param>
/// <param name="colNames">字段名</param>
/// <param name="colValues">字段名对应的数据</param>
public SQLiteDataReader 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[] + operations[] + "'" + colValues[] + "'";
for (int i = ; i < colValues.Length; i++)
{
queryString += " AND " + colNames[i] + operations[i] + "'" + colValues[i] + "'";
}
return ExecuteQuery(queryString);
} /// <summary>
/// 创建数据表
/// </summary> +
/// <returns>The table.</returns>
/// <param name="tableName">数据表名</param>
/// <param name="colNames">字段名</param>
/// <param name="colTypes">字段名类型</param>
public SQLiteDataReader CreateTable(string tableName, string[] colNames, string[] colTypes)
{
string queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "( " + colNames[] + " " + colTypes[];
for (int i = ; i < colNames.Length; i++)
{
queryString += ", " + colNames[i] + " " + colTypes[i];
}
queryString += " ) ";
return ExecuteQuery(queryString);
} /// <summary>
/// Reads the table.
/// </summary>
/// <returns>The table.</returns>
/// <param name="tableName">Table name.</param>
/// <param name="items">Items.</param>
/// <param name="colNames">Col names.</param>
/// <param name="operations">Operations.</param>
/// <param name="colValues">Col values.</param>
public SQLiteDataReader ReadTable(string tableName, string[] items, string[] colNames, string[] operations, string[] colValues)
{
string queryString = "SELECT " + items[];
for (int i = ; i < items.Length; i++)
{
queryString += ", " + items[i];
}
queryString += " FROM " + tableName + " WHERE " + colNames[] + " " + operations[] + " " + colValues[];
for (int i = ; i < colNames.Length; i++)
{
queryString += " AND " + colNames[i] + " " + operations[i] + " " + colValues[] + " ";
}
return ExecuteQuery(queryString);
} /// <summary>
/// 本类log
/// </summary>
/// <param name="s"></param>
static void Log(string s)
{
Console.WriteLine("class SqLiteHelper:::" + s);
}
}
}
控制台调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication3
{
class Program
{
private static SqLiteHelper sql;
static void Main(string[] args)
{
sql = new SqLiteHelper(@"data source=E:\620000S00000_20160707\Data.db"); //读取整张表
SQLiteDataReader reader = sql.ReadFullTable("SYS_Statistics"); while (reader.Read())
{
//读取表名
string tableName = reader.GetString(reader.GetOrdinal("TableName"));
//读取表的数据
int Rowcount = reader.GetInt32(reader.GetOrdinal("DataStatistics"));
Console.WriteLine(tableName+Rowcount);
} Console.Read();
}
}
}
注意事项:
1:引入System.Data.SQLite.dll 文件。 下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
2:引入System.Data.SQLite.dll的同时,需要将 System.Data.SQLite.dll.config 文件放在同级目录下。否则加载dll文件时报错。以及System.Data.SQLite.xml。