帮助类
using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 数据链接 { public static class Sqllite { #region 样本 private static readonly string defaultConnectString = "Data Source=" + @"../../Properties/db_name.db"; #endregion #region 变量 private static SQLiteConnection _con = null; public static string _constr = "Data Source=" + @"../../Properties/db_name.db"; #endregion #region 属性 public static string constr { get { if (_constr == null || _constr.Equals(String.Empty)) { _constr = defaultConnectString; } return _constr; } set { _constr = value; } } /// <summary> /// 获取或设置数据库连接对象 /// </summary> public static SQLiteConnection Con { get { if (Sqllite._con == null) { Sqllite._con = new SQLiteConnection(); } if (Sqllite._con.ConnectionString == null || Sqllite._con.ConnectionString.Equals(string.Empty)) { Sqllite._con.ConnectionString = Sqllite.constr; } return Sqllite._con; } set { Sqllite._con = value; } } #endregion /// <summary> /// 获取数据表 /// </summary> /// <param name="commandText">select命令</param> /// <param name="param">参数表</param> /// <returns></returns> #region MyRegion public static DataTable GetDataTable(string commandText, params SQLiteParameter[] param) { DataTable result = new DataTable(); try { using (SQLiteCommand cmd = new SQLiteCommand(commandText, Sqllite.Con)) { if (param != null) cmd.Parameters.AddRange(param); try { SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd); adapter.Fill(result); } catch (Exception ex) { result = null; } } } finally { if (Sqllite.Con.State != ConnectionState.Closed) { Sqllite.Con.Close(); } } return result; } #endregion /// <summary> /// 执行不查询的数据库操作 /// </summary> /// <param name="commandText">Oracle语句或存储过程名</param> /// <param name="commandType">Oracle命令类型</param> /// <param name="param">Oracle命令参数数组</param> /// <returns>受影响的行数</returns> #region MyRegion public static int ExecuteNonQuery(string commandText, CommandType commandType, params SQLiteParameter[] param) { int result = 0; try { using (SQLiteCommand cmd = new SQLiteCommand(commandText, Sqllite.Con)) { try { //cmd.CommandType = commandType; if (param != null) { cmd.Parameters.AddRange(param); } Sqllite.Con.Open(); result = cmd.ExecuteNonQuery(); } catch (Exception ex) { result = -1; } } } finally { if (Sqllite.Con.State != ConnectionState.Closed) { Sqllite.Con.Close(); } } return result; } #endregion } }
调用
/// <summary> /// sqlite增删改查 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { DataTable dt = new DataTable();//DataTable接收传回的数据 dt = Sqllite.GetDataTable("select * from TBL_LANDSLIP;", null);//查询 // Sqllite.ExecuteNonQuery(@"INSERT INTO ZLCB01A (ZLCB01A005) VALUES('912')", CommandType.Text, null);//增加 // Sqllite.ExecuteNonQuery("delete from ZLCB01A WHERE ZLCB01A005='955'", CommandType.Text, null);//删除 // Sqllite.ExecuteNonQuery(@"update ZLCB01A SET ZLCB01A005='955' where ZLCB01A005='912'", CommandType.Text, null);//修改 }