asp.net中配置使用Sqlite轻型数据库
Sqlite 管理工具 SQLiteDeveloper及破解
功能特点
表结构设计,数据维护,ddl生成,加密数据库支持,sqlite2,3支持
唯一缺憾,收费,有试用期
破解方法:
注册表删除 HKEY_CURRENT_USER\SharpPlus\SqliteDev 下的 StartDate 可继续使用
- protected void Page_Load(object sender, EventArgs e)
- {
- //这个文件是预先生成的数据库文件
- string sqliteFilePath = "Data Source=" + Server.MapPath("~/App_Data/demo2012.db");
- DataSet ds = new DataSet();
- MSCL.SqliteHelper sqlite = new MSCL.SqliteHelper(sqliteFilePath);
- ds = sqlite.ExecuteDataSet("Select * From LoginTable",CommandType.Text);
- gv1.DataSource = ds;
- gv1.DataBind();
- }
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data.SQLite;
- using System.Data;
- using System.Data.Common;
- namespace MSCL
- {
- /// <summary>
- /// 本类为SQLite数据库帮助类
- /// 轻量级数据库SQLite的连接字符串写法:"Data Source=D:\database\test.s3db"
- /// 轻量级数据库SQLite的加密后的连接字符串写法:"Data Source=Maximus.db;Version=3;Password=myPassword;"
- /// </summary>
- public class SqliteHelper
- {
- //数据库连接字符串
- private readonly string _conn = string.Empty;
- public SqliteHelper(string connectionString)
- {
- _conn = connectionString;
- }
- #region ExecuteNonQuery
- /// <summary>
- /// 执行数据库操作(新增、更新或删除)
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="cmd">SqlCommand对象</param>
- /// <returns>所受影响的行数</returns>
- public int ExecuteNonQuery(SQLiteCommand cmd)
- {
- int result = 0;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- using (SQLiteConnection con = new SQLiteConnection(_conn))
- {
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText);
- try
- {
- result = cmd.ExecuteNonQuery();
- trans.Commit();
- }
- catch (Exception ex)
- {
- trans.Rollback();
- throw ex;
- }
- }
- return result;
- }
- /// <summary>
- /// 执行数据库操作(新增、更新或删除)
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <returns>所受影响的行数</returns>
- public int ExecuteNonQuery(string commandText, CommandType commandType)
- {
- int result = 0;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- SQLiteCommand cmd = new SQLiteCommand();
- using (SQLiteConnection con = new SQLiteConnection(_conn))
- {
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
- try
- {
- result = cmd.ExecuteNonQuery();
- trans.Commit();
- }
- catch (Exception ex)
- {
- trans.Rollback();
- throw ex;
- }
- }
- return result;
- }
- /// <summary>
- /// 执行数据库操作(新增、更新或删除)
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <param name="cmdParms">SQL参数对象</param>
- /// <returns>所受影响的行数</returns>
- public int ExecuteNonQuery(string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
- {
- int result = 0;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- SQLiteCommand cmd = new SQLiteCommand();
- using (SQLiteConnection con = new SQLiteConnection(_conn))
- {
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, true, commandType, commandText, cmdParms);
- try
- {
- result = cmd.ExecuteNonQuery();
- trans.Commit();
- }
- catch (Exception ex)
- {
- trans.Rollback();
- throw ex;
- }
- }
- return result;
- }
- #endregion
- #region ExecuteScalar
- /// <summary>
- /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="cmd">SqlCommand对象</param>
- /// <returns>查询所得的第1行第1列数据</returns>
- public object ExecuteScalar(SQLiteCommand cmd)
- {
- object result = 0;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- using (SQLiteConnection con = new SQLiteConnection(_conn))
- {
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText);
- try
- {
- result = cmd.ExecuteScalar();
- trans.Commit();
- }
- catch (Exception ex)
- {
- trans.Rollback();
- throw ex;
- }
- }
- return result;
- }
- /// <summary>
- /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <returns>查询所得的第1行第1列数据</returns>
- public object ExecuteScalar(string commandText, CommandType commandType)
- {
- object result = 0;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- SQLiteCommand cmd = new SQLiteCommand();
- using (SQLiteConnection con = new SQLiteConnection(_conn))
- {
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
- try
- {
- result = cmd.ExecuteScalar();
- trans.Commit();
- }
- catch (Exception ex)
- {
- trans.Rollback();
- throw ex;
- }
- }
- return result;
- }
- /// <summary>
- /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <param name="cmdParms">SQL参数对象</param>
- /// <returns>查询所得的第1行第1列数据</returns>
- public object ExecuteScalar(string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
- {
- object result = 0;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- SQLiteCommand cmd = new SQLiteCommand();
- using (SQLiteConnection con = new SQLiteConnection(_conn))
- {
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, true, commandType, commandText);
- try
- {
- result = cmd.ExecuteScalar();
- trans.Commit();
- }
- catch (Exception ex)
- {
- trans.Rollback();
- throw ex;
- }
- }
- return result;
- }
- #endregion
- #region ExecuteReader
- /// <summary>
- /// 执行数据库查询,返回SqlDataReader对象
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="cmd">SqlCommand对象</param>
- /// <returns>SqlDataReader对象</returns>
- public DbDataReader ExecuteReader(SQLiteCommand cmd)
- {
- DbDataReader reader = null;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- SQLiteConnection con = new SQLiteConnection(_conn);
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText);
- try
- {
- reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return reader;
- }
- /// <summary>
- /// 执行数据库查询,返回SqlDataReader对象
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <returns>SqlDataReader对象</returns>
- public DbDataReader ExecuteReader(string commandText, CommandType commandType)
- {
- DbDataReader reader = null;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- SQLiteConnection con = new SQLiteConnection(_conn);
- SQLiteCommand cmd = new SQLiteCommand();
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, false, commandType, commandText);
- try
- {
- reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return reader;
- }
- /// <summary>
- /// 执行数据库查询,返回SqlDataReader对象
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <param name="cmdParms">SQL参数对象</param>
- /// <returns>SqlDataReader对象</returns>
- public DbDataReader ExecuteReader(string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
- {
- DbDataReader reader = null;
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- SQLiteConnection con = new SQLiteConnection(_conn);
- SQLiteCommand cmd = new SQLiteCommand();
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms);
- try
- {
- reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return reader;
- }
- #endregion
- #region ExecuteDataSet
- /// <summary>
- /// 执行数据库查询,返回DataSet对象
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="cmd">SqlCommand对象</param>
- /// <returns>DataSet对象</returns>
- public DataSet ExecuteDataSet(SQLiteCommand cmd)
- {
- DataSet ds = new DataSet();
- SQLiteConnection con = new SQLiteConnection(_conn);
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText);
- try
- {
- SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
- sda.Fill(ds);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (cmd.Connection != null)
- {
- if (cmd.Connection.State == ConnectionState.Open)
- {
- cmd.Connection.Close();
- }
- }
- }
- return ds;
- }
- /// <summary>
- /// 执行数据库查询,返回DataSet对象
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <returns>DataSet对象</returns>
- public DataSet ExecuteDataSet(string commandText, CommandType commandType)
- {
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- DataSet ds = new DataSet();
- SQLiteConnection con = new SQLiteConnection(_conn);
- SQLiteCommand cmd = new SQLiteCommand();
- SQLiteTransaction trans = null;
- PrepareCommand(cmd, con, ref trans, false, commandType, commandText);
- try
- {
- SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
- sda.Fill(ds);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (con != null)
- {
- if (con.State == ConnectionState.Open)
- {
- con.Close();
- }
- }
- }
- return ds;
- }
- /// <summary>
- /// 执行数据库查询,返回DataSet对象
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="commandText">执行语句或存储过程名</param>
- /// <param name="commandType">执行类型</param>
- /// <param name="cmdParms">SQL参数对象</param>
- /// <returns>DataSet对象</returns>
- public DataSet ExecuteDataSet(string commandText, CommandType commandType, params SQLiteParameter[] cmdParms)
- {
- if (string.IsNullOrEmpty(_conn))
- throw new ArgumentNullException("Connection string is missing.");
- if (string.IsNullOrEmpty(commandText))
- throw new ArgumentNullException("commandText");
- DataSet ds = new DataSet();
- SQLiteConnection con = null;
- SQLiteCommand cmd = new SQLiteCommand();
- SQLiteTransaction trans = null;
- try
- {
- con = new SQLiteConnection(_conn);
- PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms);
- SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
- sda.Fill(ds);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (con != null)
- {
- if (con.State == ConnectionState.Open)
- {
- con.Close();
- }
- }
- }
- return ds;
- }
- #endregion
- /// <summary>
- /// 通用分页查询方法
- /// </summary>
- /// <param name="connString">连接字符串</param>
- /// <param name="tableName">表名</param>
- /// <param name="strColumns">查询字段名</param>
- /// <param name="strWhere">where条件</param>
- /// <param name="strOrder">排序条件</param>
- /// <param name="pageSize">每页数据数量</param>
- /// <param name="currentIndex">当前页数</param>
- /// <param name="recordOut">数据总量</param>
- /// <returns>DataTable数据表</returns>
- public DataTable SelectPaging(string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut)
- {
- DataTable dt = new DataTable();
- //查询总数
- string countSql = "select count(*) from " + tableName + " where {0}";
- countSql = String.Format(countSql, strWhere);
- recordOut = Convert.ToInt32(ExecuteScalar(countSql, CommandType.Text));
- //分页
- string pagingTemplate = "select {0} from {1} where {2} order by {3} limit {4} offset {5} ";
- int offsetCount = (currentIndex - 1) * pageSize;
- string commandText = String.Format(pagingTemplate, strColumns, tableName, strWhere, strOrder, pageSize.ToString(), offsetCount.ToString());
- using (DbDataReader reader = ExecuteReader(commandText, CommandType.Text))
- {
- if (reader != null)
- {
- dt.Load(reader);
- }
- }
- return dt;
- }
- /// <summary>
- /// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
- /// </summary>
- /// <param name="cmd">Command对象</param>
- /// <param name="conn">Connection对象</param>
- /// <param name="trans">Transcation对象</param>
- /// <param name="useTrans">是否使用事务</param>
- /// <param name="cmdType">SQL字符串执行类型</param>
- /// <param name="cmdText">SQL Text</param>
- /// <param name="cmdParms">SQLiteParameters to use in the command</param>
- private void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, ref SQLiteTransaction trans, bool useTrans, CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms)
- {
- if (conn.State != ConnectionState.Open)
- conn.Open();
- cmd.Connection = conn;
- cmd.CommandText = cmdText;
- if (useTrans)
- {
- trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
- cmd.Transaction = trans;
- }
- cmd.CommandType = cmdType;
- if (cmdParms != null)
- {
- foreach (SQLiteParameter parm in cmdParms)
- cmd.Parameters.Add(parm);
- }
- }
- }
- }
asp.net中配置使用Sqlite轻型数据库的更多相关文章
- ASP.NET Core 配置 EF SQLite 支持 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 配置 EF SQLite 支持 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 配置 EF SQLite 支持 上一章节我有提 ...
- SpringBoot中配置起动时的数据库初始化角本
一.简介 我们使用SpringBoot + JPA时,需要程序在启动时执行数据表的初始化或者数据库记录的初始化.一般数据表的初始化可以通过在Spring Boot的application.proper ...
- ASP.NET中配置应用程序
1. 配置文件简介 1.1 分类 1.2关系 Machine.Config和Web.Config都是设置应用程序的配置信息,它们按照类似于继承的关系对应用程序起作用. Machine.Config ...
- 在Windows10系统中配置和运行MongoDB数据库,linux开启mongdb
参考链接:http://jingyan.baidu.com/article/11c17a2c03081ef446e39d02.html linux中开启mongodb服务: 1. 进入到/data/ ...
- asp.netMVC中配置automap
第一.新建类库,以解决方案名XXX为例,建立子类库名为 XXX.AutoMapper. 第二. XXX.AutoMapper类库中,添加对automap的引用. 第三.创建映射文件类 ModelPr ...
- 利用Advanced Installer将asp.netMVC连同IIS服务和mysql数据库一块打包成exe安装包
因为业务需要,项目中需要把asp.netmvc项目打包成exe安装程序给客户,让客户直接可以点下一步下一步安装部署web程序,并且同时要将IIS服务和mysql一同安装到服务器上,因为客户的电脑可能是 ...
- ASP.NET中的配置文件
ASP.NET中的配置文件 原创 2014年10月13日 08:15:27 1199 在机房收费系统的时候曾经应用过配置文件,当时也就那么一用对配置文件了解的不是很透彻,下面就来总结一下有关配置文 ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- 在Spring中配置SQL server 2000
前言 Lz主要目的是在Spring中配置SQL server 2000数据库,但实现目的的过程中参差着许多SQL server 2000的知识,也包罗在本文记载下来!(Lz为什么要去搞sql serv ...
随机推荐
- OpenSSL中的大数接口与基于其的自用RSA加密接口设计
本文记录了初次接触OpenSSL中的大数模块,重温了RSA加密流程,使用OpenSSL的接口包装成自用RSA加密接口,并且利用自己的接口演示了Alice与Bob通过RSA加密进行通讯的一个示例. 概览 ...
- linux下文件和目录
(1)普通文件(regular file):这是最常用的文件类型,这种文件包含了某种形式的数据,文件内容的解释由处理该文件的应用程序进行. (2)目录文件(directory file):这种文件包含 ...
- 【★】KMP算法完整教程
KMP算法完整教程 全称: Knuth_Morris_Pratt Algorithm(KMP算法) 类型: ...
- yyt
红颜迤逦隔云梯, 相思萦系解花语. 我有相思千般意, 百磨不灭铭肝肠.
- 团队作业8——Beta 阶段冲刺6th day
一.当天站立式会议 二.每个人的工作 (1)昨天已完成的工作(具体在表格中) 完善订单功能 (2)今天计划完成的工作(具体如下) 完善支付功能 (3) 工作中遇到的困难(在表格中) 成员 昨天已完成的 ...
- 第二次项目冲刺(Beta阶段)5.24
1.提供当天站立式会议照片一张 会议内容: ①检查前一天的任务情况. ②制定新一轮的任务计划. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 王婧 #63Web输出以文件名为标题 #63 ...
- 201521123022 《Java程序设计》 第九周学习总结
1.本章学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 Q1.1 截图你的提交结果(出现学号) Q1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何 ...
- JS中的DOM对象及JS对document对像的操作
DOM对象 windows:属性:opener(打开者) 方法:open().close(),setTimeout().setInterval()... location:属性:href 方法:rel ...
- ACM退役记&&回忆录
ACM退役记 2017.9.19星期二,"九一八事变"八十六年后的第二天,永远记住这个日子,刚好是我报名ACM到现在,刚好满一年,而今天正是我注册杭州电子科技大学OJ的时间(就是这 ...
- Struts2第十篇【数据校验、代码方式、XML配置方式、错误信息返回样式】
回顾以前的数据校验 使用一个FormBean对象来封装着web端来过来的数据 维护一个Map集合保存着错误信息-对各个字段进行逻辑判断 //表单提交过来的数据全都是String类型的,birthday ...