文档章节

asp.net中配置使用Sqlite轻型数据库

深圳大道
 深圳大道
发布于 2016/12/29 15:38
字数 1928
阅读 4
收藏 1

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

本文转载自:http://blog.csdn.net/smartsmile2012/article/details/9167769

深圳大道
粉丝 3
博文 877
码字总数 0
作品 0
深圳
架构师
私信 提问
Android中数据存储--采用SQLite存储数据及在SDCard中创建数据库

SQLite数据库简单的认识 SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式...

鉴客
2011/11/14
2.5K
1
C#数据本地存储方案之SQLite

即使是做网络应用,在断线情况下,也需要考虑数据的本地存储。在SQLite出现之前,数据量大的情况下,我们一直使用ACCESS,数据量小,则文件存储。ACCESS不支持事务原子性,在断电情况下(这种...

luminji
2010/12/19
0
0
dotnet core 使用 sqlite 部署到 Centos 服务器

版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问。如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博客。本文地址 https://blog.csdn.net/lindexi_gd/article/details...

lindexi_gd
2018/12/25
0
0
轻巧的本地数据库 - sqlite

  前言   SQLite,是一款轻型的数据库,它的设计目标是嵌入式的,而且现在很多嵌入式产品中使用它,它占资源非常低,在嵌入设备中,可能只需要几百K的内存就够了。它能够支持Windows/Lin...

linux运维菜
2018/11/01
0
0
PHP+SQLite与ASP(ASP.NET)+ACCESS,您选哪个?

PHP+SQLite与ASP(ASP.NET)+ACCESS您选哪个? 就一些小的企业网站和应用而言,两种方式已经足够支撑其应用了。 很多多功能空间都支持ASP+ACCESS,也支持PHP+SQLite; 就性能来说,PHP+SQLite并...

Jankrong
2011/08/05
2.3K
14

没有更多内容

加载失败,请刷新页面

加载更多

UserInputControls用户输入控制

enum UserInputControls { kGovernedByOrthoMode = 0x0001,//正交模式管理 kNullResponseAccepted = 0x0002,//允许输入空 kDontEchoCancelForCtrlC = 0x0004,//ctrl C 模式不能重复......

一个小妞
30分钟前
1
0
分布式系统的事务处理

当我们在生产线上用一台服务器来提供数据服务的时候,我会遇到如下的两个问题: 1)一台服务器的性能不足以提供足够的能力服务于所有的网络请求。 2)我们总是害怕我们的这台服务器停机,造成...

群星纪元
32分钟前
5
0
Kanban VS Scrum:哪个是最好的敏捷项目管理框架

“我们使用敏捷开发。”在与软件开发团队交流时,你会听到很多这样的说法。根据统计,2018年全球约有90%的开发人员在使用敏捷开发。Choerodon猪齿鱼团队也是其中之一。 但是,敏捷并不统一。...

Choerodon
36分钟前
3
0
vue select 慢一步

我在使用vue的select的时候,@change事件里面,写一个axios拿到数据,然后修改某个变量,按理修改变量会立即呈现在页面上面,但实际情况是,值是变更了,但是页面上的效果慢了一步,非常奇怪...

朝如青丝暮成雪
41分钟前
2
0
分布式架构 核心知识体系

1.问题 1、何为分布式何为微服务? 2、为什么需要分布式? 3、分布式核心理论基础,节点、网络、时间、顺序,一致性? 4、分布式是系统有哪些设计模式? 5、分布式有哪些类型? 6、如何实现分...

小刀爱编程
43分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部