c#连接SQLite
SQLite这个精巧的小数据库,无需安装软件,只需要一个System.Data.SQLite.DLL文件即可操作SQLite数据库。SQLite是一个开源数据库,现在已变得越来越流行,它的体积很小,被广泛应用于各种不同类型的应用中。SQLite已经是世界上布署得最广泛的SQL数据库引擎,被用在无以计数的桌面电脑应用中,还有消费电子设备中,如移动电话、掌上电脑和MP3播放器等。SQLite的源码就放在公有领域(即WikiPedia的public domain)中。
本代码为网上收录的sqlite数据库操作类。
代码正文
Codeusing System;using System.Collections.Generic;using System.Text;using System.Web;using System.Configuration;using System.Data;using System.Data.SQLite;namespace DAL{public class Sqlite{/// <summary>/// 获得连接对象/// </summary>/// <returns></returns>public static SQLiteConnection GetSQLiteConnection(){return new SQLiteConnection("Data Source=" + System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["db"].ToString()));}private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p){if (conn.State != ConnectionState.Open)conn.Open();cmd.Parameters.Clear();cmd.Connection = conn;cmd.CommandText = cmdText;cmd.CommandType = CommandType.Text;cmd.CommandTimeout = 30;if (p != null){foreach (object parm in p)cmd.Parameters.AddWithValue(string.Empty, parm);//for (int i = 0; i < p.Length; i++)// cmd.Parameters[i].Value = p[i];}}public static DataSet ExecuteDataset(string cmdText, params object[] p){DataSet ds = new DataSet();SQLiteCommand command = new SQLiteCommand();using (SQLiteConnection connection = GetSQLiteConnection()){PrepareCommand(command, connection, cmdText, p);SQLiteDataAdapter da = new SQLiteDataAdapter(command);da.Fill(ds);}return ds;}public static DataRow ExecuteDataRow(string cmdText, params object[] p){DataSet ds = ExecuteDataset(cmdText, p);if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)return ds.Tables[0].Rows[0];return null;}/// <summary>/// 返回受影响的行数/// </summary>/// <param name="cmdText">a</param>/// <param name="commandParameters">传入的参数</param>/// <returns></returns>public static int ExecuteNonQuery(string cmdText, params object[] p){SQLiteCommand command = new SQLiteCommand();using (SQLiteConnection connection = GetSQLiteConnection()){PrepareCommand(command, connection, cmdText, p);return command.ExecuteNonQuery();}}/// <summary>/// 返回SqlDataReader对象/// </summary>/// <param name="cmdText"></param>/// <param name="commandParameters">传入的参数</param>/// <returns></returns>public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p){SQLiteCommand command = new SQLiteCommand();SQLiteConnection connection = GetSQLiteConnection();try{PrepareCommand(command, connection, cmdText, p);SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);return reader;}catch{connection.Close();throw;}}/// <summary>/// 返回结果集中的第一行第一列,忽略其他行或列/// </summary>/// <param name="cmdText"></param>/// <param name="commandParameters">传入的参数</param>/// <returns></returns>public static object ExecuteScalar(string cmdText, params object[] p){SQLiteCommand cmd = new SQLiteCommand();using (SQLiteConnection connection = GetSQLiteConnection()){PrepareCommand(cmd, connection, cmdText, p);return cmd.ExecuteScalar();}}/// <summary>/// 分页/// </summary>/// <param name="recordCount"></param>/// <param name="pageIndex"></param>/// <param name="pageSize"></param>/// <param name="cmdText"></param>/// <param name="countText"></param>/// <param name="p"></param>/// <returns></returns>public static DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, params object[] p){if (recordCount < 0)recordCount = int.Parse(ExecuteScalar(countText, p).ToString());DataSet ds = new DataSet();SQLiteCommand command = new SQLiteCommand();using (SQLiteConnection connection = GetSQLiteConnection()){PrepareCommand(command, connection, cmdText, p);SQLiteDataAdapter da = new SQLiteDataAdapter(command);da.Fill(ds, (pageIndex - 1) * pageSize, pageSize, "result");}return ds;}}}
System.Data.SQLite数据库通用类
针对对数据库的操作情况,分为以下几种情况:
创建数据库文件;
返回DataTable;
返回DataReader;
执行增删改,返回受影响的行数;
执行查询,返回第一行第一列(通常用于带有行函数的查询,如SUM/AVG/COUNT等);
返回库中所有的表;
因为在System.Data.SQLite中不存在存储过程,所以所有的操作都是基于文本的SQL语句,为了避免SQL注入,所以使用了参数化的SQL语句,这个数据库通用类如下:
Codeusing System; using System.Data; using System.Data.Common; using System.Data.SQLite;namespace SQLiteQueryBrowser {/// <summary>/// 说明:这是一个针对System.Data.SQLite的数据库常规操作封装的通用类。/// 作者:zhoufoxcn(周公)/// 日期:2010-04-01/// Blog:http://zhoufoxcn.blog.51cto.com or http://blog.csdn.net/zhoufoxcn/// Version:0.1/// </summary>public class SQLiteDBHelper{private string connectionString = string.Empty;/// <summary>/// 构造函数/// </summary>/// <param name="dbPath">SQLite数据库文件路径</param>public SQLiteDBHelper(string dbPath){this.connectionString = "Data Source=" + dbPath;}/// <summary>/// 创建SQLite数据库文件/// </summary>/// <param name="dbPath">要创建的SQLite数据库文件路径</param>public static void CreateDB(string dbPath){using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbPath)){connection.Open();using (SQLiteCommand command = new SQLiteCommand(connection)){command.CommandText = "CREATE TABLE Demo(id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE)";command.ExecuteNonQuery();command.CommandText = "DROP TABLE Demo";command.ExecuteNonQuery();}}}/// <summary>/// 对SQLite数据库执行增删改操作,返回受影响的行数。/// </summary>/// <param name="sql">要执行的增删改的SQL语句</param>/// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>/// <returns></returns>public int ExecuteNonQuery(string sql, SQLiteParameter[] parameters){int affectedRows = 0;using (SQLiteConnection connection = new SQLiteConnection(connectionString)){connection.Open();using (DbTransaction transaction = connection.BeginTransaction()){using (SQLiteCommand command = new SQLiteCommand(connection)){command.CommandText = sql;if (parameters != null){command.Parameters.AddRange(parameters);}affectedRows = command.ExecuteNonQuery();}transaction.Commit();}}return affectedRows;}/// <summary>/// 执行一个查询语句,返回一个关联的SQLiteDataReader实例/// </summary>/// <param name="sql">要执行的查询语句</param>/// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>/// <returns></returns>public SQLiteDataReader ExecuteReader(string sql, SQLiteParameter[] parameters){SQLiteConnection connection = new SQLiteConnection(connectionString);SQLiteCommand command = new SQLiteCommand(sql, connection);if (parameters != null){command.Parameters.AddRange(parameters);}connection.Open();return command.ExecuteReader(CommandBehavior.CloseConnection);}/// <summary>/// 执行一个查询语句,返回一个包含查询结果的DataTable/// </summary>/// <param name="sql">要执行的查询语句</param>/// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>/// <returns></returns>public DataTable ExecuteDataTable(string sql, SQLiteParameter[] parameters){using (SQLiteConnection connection = new SQLiteConnection(connectionString)){using (SQLiteCommand command = new SQLiteCommand(sql, connection)){if (parameters != null){command.Parameters.AddRange(parameters);}SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);DataTable data = new DataTable();adapter.Fill(data);return data;}}}/// <summary>/// 执行一个查询语句,返回查询结果的第一行第一列/// </summary>/// <param name="sql">要执行的查询语句</param>/// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>/// <returns></returns>public Object ExecuteScalar(string sql, SQLiteParameter[] parameters){using (SQLiteConnection connection = new SQLiteConnection(connectionString)){using (SQLiteCommand command = new SQLiteCommand(sql, connection)){if (parameters != null){command.Parameters.AddRange(parameters);}SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);DataTable data = new DataTable();adapter.Fill(data);return data;}}}/// <summary>/// 查询数据库中的所有数据类型信息/// </summary>/// <returns></returns>public DataTable GetSchema(){using (SQLiteConnection connection = new SQLiteConnection(connectionString)){connection.Open();DataTable data=connection.GetSchema("TABLES");connection.Close();//foreach (DataColumn column in data.Columns)//{// Console.WriteLine(column.ColumnName);//}return data;}}} }