using System;using System.Collections.Generic;using System.Data;using System.Data.SQLite;namespace EasyEnergy.AppLogicService{ public class AlarmSqliteDbHelp { public static string ConnSqlLiteDbPath = @"D:\data\test.data"; public static string ConnString { get { return string.Format("Data Source={0}", ConnSqlLiteDbPath); } } public static int ExecuteRefId(string sql) { SQLiteConnection connection; SQLiteTransaction transaction = null; try { connection = new SQLiteConnection(ConnString); connection.Open(); var command = new SQLiteCommand(connection) { CommandText = sql + ";SELECT last_insert_rowid()" }; return Convert.ToInt32(command.ExecuteScalar()); } catch (Exception) { transaction.Rollback(); throw; } } public static int Execute(string sSQL, bool bUseTransaction = false) { int num; SQLiteConnection connection; //if (!bUseTransaction) //{ // connection = new SQLiteConnection(ConnString); // connection.Open(); // var command = new SQLiteCommand(connection) { // CommandText = sSQL // }; // return command.ExecuteNonQuery(); //} SQLiteTransaction transaction = null; try { connection = new SQLiteConnection(ConnString); connection.Open(); transaction = connection.BeginTransaction(); num = new SQLiteCommand(connection) { CommandText = sSQL }.ExecuteNonQuery(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } return num; } public static DataTable GetDataTable(string sSQL) { var connection = new SQLiteConnection(ConnString); connection.Open(); var command = new SQLiteCommand { CommandText = sSQL, Connection = connection }; var adapter = new SQLiteDataAdapter(command); var dataTable = new DataTable("table"); adapter.Fill(dataTable); return dataTable; } public static int GetMaxId(string sKeyField, string sTableName) { DataTable dataTable = GetDataTable("select ifnull(max([" + sKeyField + "]),0) as MaxID from [" + sTableName + "]"); if ((dataTable != null) && (dataTable.Rows.Count > 0)) { return Convert.ToInt32(dataTable.Rows[0][0].ToString()); } return 0; } public static object GetSingle(string sSQL) { DataTable dataTable = GetDataTable(sSQL); if ((dataTable != null) && (dataTable.Rows.Count > 0)) { return dataTable.Rows[0][0]; } return null; } }}