using System; using System.Collections.Generic; using System.Data.SQLite; using System.Linq; using System.Text; using System.IO; using System.Data; using System.Configuration; namespace OTSModelSharp.DTLBase { namespace Helper { public class SQLiteHelper3 { /// /// 创建数据库文件 /// /// 文件名 public static void CreateDBFile(string fileName) { string path = Environment.CurrentDirectory + @"/Data/"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string databaseFileName = path + fileName; if (!File.Exists(databaseFileName)) { SQLiteConnection.CreateFile(databaseFileName); } } /// /// 删除数据库 /// /// 文件名 public static void DeleteDBFile(string fileName) { string path = Environment.CurrentDirectory + @"/Data/"; if (File.Exists(path)) { File.Delete(path); } } /// /// 生成连接字符串 /// /// private static string CreateConnectionString() { SQLiteConnectionStringBuilder connectionString = new SQLiteConnectionStringBuilder(); //connectionString.DataSource = @"data/data.db";//此处文件名可以使用变量表示 connectionString.DataSource = ConfigurationManager.ConnectionStrings["SQLite"].ConnectionString; string conStr = connectionString.ToString(); return conStr; } private static SQLiteConnection m_dbConnection; /// /// 事务 /// public static SQLiteTransaction trans = null; #region 事务控制 /// /// 开始事务 /// public static void BeginTransaction() { trans = m_dbConnection.BeginTransaction(IsolationLevel.ReadCommitted); } /// /// 回滚事务 /// public static void Rollback() { trans.Rollback(); } /// /// 提交事务 /// public static void Commit() { trans.Commit(); } #endregion /// /// 连接到数据库 /// /// public static SQLiteConnection dbConnection() { m_dbConnection = new SQLiteConnection(CreateConnectionString()); m_dbConnection.Open(); return m_dbConnection; } /// /// 在指定数据库中创建一个table /// /// sql语言,如:create table pat_info (name varchar(20), score int) /// public static bool CreateTable(string sql) { try { SQLiteCommand command = new SQLiteCommand(sql, dbConnection()); command.ExecuteNonQuery(); return true; } catch (Exception ex) { //_ErrorLog.Insert("ExecuteNonQuery(" + sql + ")Err:" + ex); return false; } finally { closeConn(); } } /// /// 在指定数据库中删除一个table /// /// 表名称 /// public static bool DeleteTable(string tablename) { try { SQLiteCommand cmd = new SQLiteCommand("DROP TABLE IF EXISTS " + tablename, dbConnection()); cmd.ExecuteNonQuery(); return true; } catch (Exception ex) { //_ErrorLog.Insert("ExecuteNonQuery(DROP TABLE IF EXISTS " + tablename + ")Err:" + ex); return false; } finally { closeConn(); } } /// /// 在指定表中添加列 /// /// 表名 /// 列名 /// 列的数值类型 /// public static bool AddColumn(string tablename, string columnname, string ctype) { try { SQLiteCommand cmd = new SQLiteCommand("ALTER TABLE " + tablename + " ADD COLUMN " + columnname + " " + ctype, dbConnection()); cmd.ExecuteNonQuery(); return true; } catch (Exception ex) { //_ErrorLog.Insert("ExecuteNonQuery(ALTER TABLE " + tablename + " ADD COLUMN " + columnname + " " + ctype + ")Err:" + ex); return false; } finally { closeConn(); } } /// /// 执行增删改查操作 /// /// 查询语言 /// public static int ExecuteNonQuery(string sql) { try { SQLiteCommand cmd; cmd = new SQLiteCommand(sql, dbConnection()); cmd.ExecuteNonQuery().ToString(); return 1; } catch (Exception ex) { //_ErrorLog.Insert("ExecuteNonQuery(" + sql + ")Err:" + ex); return 0; } finally { closeConn(); } } /// /// 执行SQL,返回受影响的行数 /// 可用于执行表创建语句 /// paramArr == null 表示无参数 /// /// /// public static int ExecuteNonQuery(string sql, SQLiteParameter[] paramArr) { if (sql == null) { throw new ArgumentNullException("sql=null"); } SQLiteCommand cmd = new SQLiteCommand(); cmd.CommandText = sql; if (paramArr != null) { foreach (SQLiteParameter p in paramArr) { cmd.Parameters.Add(p); } } cmd.Connection = m_dbConnection; int i = cmd.ExecuteNonQuery(); return i; } /// /// 返回一条记录查询 /// /// sql查询语言 /// 返回字符串数组 public static string[] SqlRow(string sql) { try { SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句 SQLiteDataReader reader = sqlcmd.ExecuteReader(); if (!reader.Read()) { return null; } string[] Row = new string[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { Row[i] = (reader[i].ToString()); } reader.Close(); return Row; } catch (Exception ex) { //_ErrorLog.Insert("SqlRow(" + sql + ")Err:" + ex); return null; } finally { closeConn(); } } /// /// 唯一结果查询 /// /// sql查询语言 /// 返回一个字符串 public static string sqlone(string sql) { try { SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句 return sqlcmd.ExecuteScalar().ToString(); } catch { return ""; } finally { closeConn(); } } /// /// 获取一列数据 /// /// 单列查询 /// 返回结果数量 /// 返回一个数组 public static List sqlcolumn(string sql) { try { List Column = new List(); SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句 SQLiteDataReader reader = sqlcmd.ExecuteReader(); while (reader.Read()) { Column.Add(reader[0].ToString()); } reader.Close(); return Column; } catch (Exception ex) { //_ErrorLog.Insert("sqlcolumn(" + sql + ")Err:" + ex); return null; } finally { closeConn(); } } /// /// 返回记录集查询 /// /// sql查询语言 /// 返回查询结果集 public static DataTable SqlTable(string sql) { try { SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句 sqlcmd.CommandTimeout = 120; SQLiteDataReader reader = sqlcmd.ExecuteReader(); DataTable dt = new DataTable(); if (reader != null) { dt.Load(reader, LoadOption.PreserveChanges, null); } return dt; } catch (Exception ex) { //_ErrorLog.Insert("SqlReader(" + sql + ")Err:" + ex); return null; } finally { closeConn(); } } /// /// 关闭数据库连接 /// public static void closeConn() { try { if (m_dbConnection.State == ConnectionState.Open) m_dbConnection.Close(); else if (m_dbConnection.State == ConnectionState.Broken) { m_dbConnection.Close(); } } catch (Exception ex) { //_ErrorLog.Insert("closeConnErr:" + ex); } } } } }