数据库帮助类

 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();
}
}
}

sqlhelper 数据库帮助操作类的更多相关文章

  1. Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类

    Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类 转载至:https://blog.csdn.net/fishinhouse/article/details ...

  2. [转]html5 js 访问 sqlite 数据库的操作类

    本文转自:http://blog.csdn.net/tsxw24/article/details/7613815 webkit 核心的浏览器提供了 3个 api接口,用于访问本地sqlite数据,但使 ...

  3. Java 数据库简单操作类

    数据库操作类,将所有连接数据库的配置信息以及基本的CRUD操作封装在一个类里,方便项目里使用,将连接数据库的基本信息放在配置文件 "dbinfo.properties" 中,通过类 ...

  4. fastCMS数据库相关操作类

    fastCMS针对数据库的操作有以下几个类: 1.[paging_Class]分页类 此类用于分页检索数据库内符合条件的记录 1) 支持百万级数据分页 2) 支持多种类型的SQL语法,比如 Left ...

  5. ACCESS数据库C#操作类(包含事务)

    转自http://blog.csdn.net/allen3010/article/details/6336717 这个是针对ACCESS数据库操作的类,同样也是从SQLHELPER提取而来,分页程序的 ...

  6. Express框架使用以及数据库公共操作类整理(Win7下的NodeJs)

    具体步骤: 1.安装开发工具WebStorm: 2.安装node/npm(下载地址:https://nodejs.org/download/)选择适合你的xxx.mis安装: 3.安装express框 ...

  7. Java通过代理类实现数据库DAO操作

    下面的所有代码示例都取自李兴华的<Java Web开发实战经典>的随书源码,因为觉得设计得很好,所以将代码摘录下来作成笔记. 首先,我们在一个java文件中定义要存储的结构类型: impo ...

  8. 自己封装的ASP.NET的MYSQL的数据库操作类

    /** * 作者:牛腩 * 创建时间:2010年3月7日17时35分 * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MyS ...

  9. C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

    原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...

随机推荐

  1. WEBGL学习【七】画布绘图

    主要是对WEBGL的绘图部分进行了进一步加强的认识和理解 <!DOCTYPE HTML> <html lang="en"> <head> < ...

  2. Linux 获取帮助

    Linux中获取帮助的方法  方法:    COMMAND --help 或者 help COMMAND    whatis    man或info    本地帮助文档 /usr/share/doc/ ...

  3. Linux安装expect命令

    [Linux安装expect命令]:--expect是在Tcl基础上创建起来的,所以在安装expect前我们应该先安装Tcl.①:tcl安装源码下载:http://www.tcl.tk/softwar ...

  4. EasyUI闪屏,EasyUI页面加载提示:原理+代码+效果图

    使用EasyUI时,有个经常遇到的问题,页面还没有渲染完成的时候,就展现了. 刚刚开始很混乱,等加载完成后,就好了. 参考这篇文章http://blog.csdn.net/zheng0518/arti ...

  5. eclipse debug的时候提示debug Edit Source Lookup path

    原因可能是代码资源包未加载到debug的路径中,解决方法如下: Debug 视图下 ->在调试的线程上 右键单击 ->选择Edit Source Lookup Path ->选择Ad ...

  6. HDU5514 Frogs

    /* HDU5514 Frogs http://acm.hdu.edu.cn/showproblem.php?pid=5514 容斥原理 * * */ #include <cstdio> ...

  7. rabbitMQ学习笔记(二) 简单的发送与接收消息 HelloWorld

    首先要下载rabbitmq的javaClient库,然后加入到项目中,下载地址为:http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1. ...

  8. 小P寻宝记——好基友一起走

    小P寻宝记--好基友一起走 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 话说.上次小P到伊利哇呀国旅行得到了一批宝藏.他是 ...

  9. 通过PowerShell卸载全部的SharePoint 2010 解决方式

    通过PowerShell卸载全部的SharePoint 2010 解决方式         为了演示.我常常须要拆毁再重建SharePoint 2010 环境.         我常常须要用到的操作就 ...

  10. @Autowired 凝视遇到的问题,@Qualifier 帮助解决这个问题

    当候选 Bean 数目不为 1 时的应对方法 在默认情况下使用 @Autowired 凝视进行自己主动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个. 当找不到一个匹配的 Be ...