using System.Data; using System.IO; using System.Windows.Forms; using System.Drawing; using System.Data.SqlClient; using System; using System.Text; using System.Data.OleDb; namespace FlyDockTool { public class accessOptions { public OleDbConnection SqlConn = null; /// /// 让此类创建一个单例类 /// public static accessOptions Instance = null;//申明一个EcgDrawing对象,复制Null private static readonly object LockHelper = new object(); public static accessOptions CreateInstance(string accessDbPath) { if (Instance == null) { lock (LockHelper) { if (Instance == null) Instance = new accessOptions(accessDbPath); } } return Instance; } public accessOptions(string accessDbPath) { GetSqlConnection(accessDbPath); } /// /// 获得sqlite数据库连接 /// /// public bool GetSqlConnection(string accessDbPath) { try { string ConnectionStr = "Provider=Microsoft.Jet.OleDb.4.0;"; ConnectionStr += @"Data Source="+ accessDbPath + ""; OleDbConnection conn = new OleDbConnection(ConnectionStr); SqlConn = conn; } catch { return false; } return true; } /// /// 执行sqlite查询语句,并返回一个 DataTable /// /// /// public DataTable ExcuteSql(string sqliteStr) { try { if (SqlConn.State == ConnectionState.Closed) { SqlConn.Open(); } var sqliteDp = new OleDbDataAdapter(sqliteStr, SqlConn) {SelectCommand = {CommandTimeout = 600000}}; var sqliteds = new DataSet(); sqliteDp.Fill(sqliteds); return sqliteds.Tables[0]; } catch(Exception ex) { //byte[] ecgDataArray = Encoding.UTF8.GetBytes(sqliteStr.ToString() + "\r\n"+ex.ToString()+"\r\n"); //ecgFileWrite(ecgDataArray, @"d:\log.txt"); return null; } } private void ecgFileWrite(byte[] ecgDataArray, string FilePath) { FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate); fs.Position = fs.Length; //开始写入 fs.Write(ecgDataArray, 0, ecgDataArray.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); } /// /// Sqlite 添加数据的操作函数 /// /// /// public bool SqlAdd(string sqliteSQL) { try { if (SqlConn.State == ConnectionState.Closed) SqlConn.Open(); var sqliteCmd = new OleDbCommand(sqliteSQL, SqlConn); sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); SqlConn.Close(); } catch { return false; } return true; } /// /// Sqlite 删除数据的操作函数 /// /// /// public bool SqlDelete(string sqliteSQL) { try { if (SqlConn.State == ConnectionState.Closed) SqlConn.Open(); var sqliteCmd = new OleDbCommand(sqliteSQL, SqlConn); sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); SqlConn.Close(); } catch { return false; } return true; } /// /// Sqlite 更新数据的操作函数 /// /// /// public bool SqlUpdate(string sqliteSQL) { try { if (SqlConn.State == ConnectionState.Closed) SqlConn.Open(); var sqliteCmd = new OleDbCommand(sqliteSQL, SqlConn); sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); SqlConn.Close(); } catch { return false; } return true; } /// /// 执行SQL语句函数 /// /// SQL语句 /// SQL语句中的参数组 /// 返回受影响记录条数 public int ExecuteSql(string sqlcmd, params SqlParameter[] paras) { var cmd = new OleDbCommand(sqlcmd, SqlConn); if (SqlConn.State == ConnectionState.Closed) { SqlConn.Open(); } foreach (SqlParameter p in paras) { cmd.Parameters.Add(p); } int cnt = cmd.ExecuteNonQuery(); SqlConn.Close(); return cnt; } /// /// Sqlite 执行数据库的多条语句, /// /// /// public bool SqlExecuteNonQuery(string[] sqliteSQL) { if (SqlConn.State == ConnectionState.Closed) { SqlConn.Open(); } OleDbTransaction sqlTran = SqlConn.BeginTransaction(); try { foreach (string sql in sqliteSQL) { var sqliteCmd = new OleDbCommand(sql, SqlConn, sqlTran); int i = sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); } sqlTran.Commit(); } catch { sqlTran.Rollback(); return false; } finally { SqlConn.Close(); } return true; } public Image GetImg(byte[] imgBytes) { var ms = new MemoryStream(imgBytes); Image img = Image.FromStream(ms); return img; } } }