绍兴建站模板厂家,做的好的地方网站,电子商务网站建设与管理案例,公司模板网站建设我们在开发应用是经常会需要用到一些数据的存储#xff0c;存储的方式有多种#xff0c;使用数据库是一种比较受大家欢迎的方式。但是对于一些小型的应用#xff0c;如一些移动APP#xff0c;通常的数据库过于庞大#xff0c;而轻便的SQLite则能解决这一问题。不但操作方便…我们在开发应用是经常会需要用到一些数据的存储存储的方式有多种使用数据库是一种比较受大家欢迎的方式。但是对于一些小型的应用如一些移动APP通常的数据库过于庞大而轻便的SQLite则能解决这一问题。不但操作方便而且只需要要一个文件即可在这里我们来说一说使用C#语言操作SQLite数据库。
1、SQLite简介
SQLite是一款轻型的数据库是遵守ACID的关系型数据库管理系统它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的而且目前已经在很多嵌入式产品中使用了它它占用资源非常的低在嵌入式设备中可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统同时能够跟很多程序语言相结合比如 Tcl、C#、PHP、Java等还有ODBC接口同样比起MySQL、PostgreSQL这两款开源的世界著名数据库管理系统来讲它的处理速度比他们都快。
如果想了解更多关于SQLite的问题可以访问它的官方网站http://www.sqlite.org/
2、开始前的准备
在开始之前我们需要准备必要的开发环境这次咱们使用的是Visual Studio 2015开发环境但是我们开发基于SQLite的应用光有VS2015还不够。我需要到SQLite的官方网站下载并安装SQLite。
在SQLite官网找到下载有应用于各种环境的SQLite组件及源码我们选择Precompiled Binaries for .NET这是应用于.NET开发环境的点击进入会看到应用于.NET2.0直至4.6以及32位和64位平台的各个版本。我们选择Setups for 32-bit Windows (.NET Framework 4.6)下载安装即可。
3、C#操作SQLite的封装
在完成开发环境的准备之后我们接下来实现对SQLite操作的必要封装以进一步降低在具体应用中的使用难度。在这里我们只是封装一些常用而且必要的功能。 public class SQLiteHelper{//创建数据库文件public static void CreateDBFile(string fileName){string path System.Environment.CurrentDirectory /Data/;if (!Directory.Exists(path)){Directory.CreateDirectory(path);}string databaseFileName path fileName;if (!File.Exists(databaseFileName)){SQLiteConnection.CreateFile(databaseFileName);}}//生成连接字符串private static string CreateConnectionString(){SQLiteConnectionStringBuilder connectionString new SQLiteConnectionStringBuilder();connectionString.DataSource data/ScriptHelper.db;string conStr connectionString.ToString();return conStr;}/// summary/// 对插入到数据库中的空值进行处理/// /summary/// param namevalue/param/// returns/returnspublic static object ToDbValue(object value){if (value null){return DBNull.Value;}else{return value;}}/// summary/// 对从数据库中读取的空值进行处理/// /summary/// param namevalue/param/// returns/returnspublic static object FromDbValue(object value){if (value DBNull.Value){return null;}else{return value;}}/// summary/// 执行非查询的数据库操作/// /summary/// param namesqlString要执行的sql语句/param/// param nameparameters参数列表/param/// returns返回受影响的条数/returnspublic static int ExecuteNonQuery(string sqlString, params SQLiteParameter[] parameters){string connectionStringCreateConnectionString();using (SQLiteConnection conn new SQLiteConnection(connectionString)){conn.Open();using (SQLiteCommand cmd conn.CreateCommand()){cmd.CommandText sqlString;foreach (SQLiteParameter parameter in parameters){cmd.Parameters.Add(parameter);}return cmd.ExecuteNonQuery();}}}/// summary/// 执行查询并返回查询结果第一行第一列/// /summary/// param namesqlStringSQL语句/param/// param namesqlparams参数列表/param/// returns/returnspublic static object ExecuteScalar(string sqlString, params SQLiteParameter[] parameters){string connectionString CreateConnectionString();using (SQLiteConnection conn new SQLiteConnection(connectionString)){conn.Open();using (SQLiteCommand cmd conn.CreateCommand()){cmd.CommandText sqlString;foreach (SQLiteParameter parameter in parameters){cmd.Parameters.Add(parameter);}return cmd.ExecuteScalar();}}}/// summary/// 查询多条数据/// /summary/// param namesqlStringSQL语句/param/// param nameparameters参数列表/param/// returns返回查询的数据表/returnspublic static DataTable GetDataTable(string sqlString,params SQLiteParameter[] parameters){string connectionString CreateConnectionString();using (SQLiteConnection conn new SQLiteConnection(connectionString)){conn.Open();using (SQLiteCommand cmd conn.CreateCommand()){cmd.CommandText sqlString;foreach (SQLiteParameter parameter in parameters){cmd.Parameters.Add(parameter);}DataSet ds new DataSet();SQLiteDataAdapter adapter new SQLiteDataAdapter(cmd);adapter.Fill(ds);return ds.Tables[0];}}}}
4、应用实例
上面封装完了之后我们就是使用上面封装的函数来实际操作SQLite数据库。对数据库的应用实例无非就是对各种对象的增、删、改、查。我没列举一个对源码类型对象的各种操作实例如下 public class ScriptTypeDAL{public ScriptTypeM ToScriptType(DataRow row){ScriptTypeM type new ScriptTypeM();type.ScriptTypeId (Guid)row[ScriptTypeId];type.ScriptType (string)row[ScriptType];type.IsUsing (bool)row[IsUsing];return type;}public void Insert(ScriptTypeM Type){SQLiteHelper.ExecuteNonQuery(insert into TB_ScriptType(ScriptTypeId,ScriptType,IsUsing)Values(ScriptTypeId,ScriptType,1),new SQLiteParameter(ScriptTypeId, Type.ScriptTypeId),new SQLiteParameter(ScriptType, Type.ScriptType));}public void Update(ScriptTypeM Type){SQLiteHelper.ExecuteNonQuery(update TB_ScriptType set ScriptTypeScriptType,IsUsingIsUsing where ScriptTypeIdScriptTypeId,new SQLiteParameter(ScriptType, Type.ScriptType),new SQLiteParameter(IsUsing, Type.IsUsing),new SQLiteParameter(ScriptTypeId, Type.ScriptTypeId));}public ScriptTypeM GetbyId(Guid id){DataTable table SQLiteHelper.GetDataTable(select * from TB_ScriptType where ScriptTypeIdid,new SQLiteParameter(id, id));if (table.Rows.Count 0){return null;}else if (table.Rows.Count 1){throw new Exception(Id重复);}else{return ToScriptType(table.Rows[0]);}}public ScriptTypeM GetbyName(string name){DataTable table SQLiteHelper.GetDataTable(select * from TB_ScriptType where ScriptTypename,new SQLiteParameter(name, name));if (table.Rows.Count 0){return null;}else if (table.Rows.Count 1){throw new Exception(类型名称重复);}else{return ToScriptType(table.Rows[0]);}}public ScriptTypeM[] ListAll(){DataTable table SQLiteHelper.GetDataTable(select * from TB_ScriptType where IsUsing1);ScriptTypeM[] type new ScriptTypeM[table.Rows.Count];for (int i 0; i table.Rows.Count; i){type[i] ToScriptType(table.Rows[i]);}return type;}public ScriptTypeM[] Search(string sql, ListSQLiteParameter parameterList){DataTable table SQLiteHelper.GetDataTable(sql, parameterList.ToArray());ScriptTypeM[] type new ScriptTypeM[table.Rows.Count];for (int i 0; i table.Rows.Count; i){type[i] ToScriptType(table.Rows[i]);}return type;}}
SQLite数据库小巧而且应用方便在一些小型应用和嵌入式应用中很有优势当然如何应用的得心应手就看个人了。
欢迎关注