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

2016-12-30 09:56:05来源:oschina作者:深圳大道人点击

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
{
///
/// 本类为SQLite数据库帮助类
/// 轻量级数据库SQLite的连接字符串写法:"Data Source=D:/database/test.s3db"
/// 轻量级数据库SQLite的加密后的连接字符串写法:"Data Source=Maximus.db;Version=3;Password=myPassword;"
///

public class SqliteHelper
{
//数据库连接字符串
private readonly string _conn = string.Empty;
public SqliteHelper(string connectionString)
{
_conn = connectionString;
}
#region ExecuteNonQuery
///
/// 执行数据库操作(新增、更新或删除)
///

/// 连接字符串
/// SqlCommand对象
/// 所受影响的行数
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;
}
///
/// 执行数据库操作(新增、更新或删除)
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// 所受影响的行数
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;
}
///
/// 执行数据库操作(新增、更新或删除)
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// 所受影响的行数
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
///
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
///

/// 连接字符串
/// SqlCommand对象
/// 查询所得的第1行第1列数据
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;
}
///
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// 查询所得的第1行第1列数据
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;
}
///
/// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// 查询所得的第1行第1列数据
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
///
/// 执行数据库查询,返回SqlDataReader对象
///

/// 连接字符串
/// SqlCommand对象
/// SqlDataReader对象
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;
}
///
/// 执行数据库查询,返回SqlDataReader对象
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SqlDataReader对象
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;
}
///
/// 执行数据库查询,返回SqlDataReader对象
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// SqlDataReader对象
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
///
/// 执行数据库查询,返回DataSet对象
///

/// 连接字符串
/// SqlCommand对象
/// DataSet对象
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;
}
///
/// 执行数据库查询,返回DataSet对象
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// DataSet对象
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;
}
///
/// 执行数据库查询,返回DataSet对象
///

/// 连接字符串
/// 执行语句或存储过程名
/// 执行类型
/// SQL参数对象
/// DataSet对象
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
///
/// 通用分页查询方法
///

/// 连接字符串
/// 表名
/// 查询字段名
/// where条件
/// 排序条件
/// 每页数据数量
/// 当前页数
/// 数据总量
/// DataTable数据表
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;
}
///
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
///

/// Command对象
/// Connection对象
/// Transcation对象
/// 是否使用事务
/// SQL字符串执行类型
/// SQL Text
/// SQLiteParameters to use in the command
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);
}
}
}
}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台