using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace _1200Gxml心电图绘制 { public class SqliteOptions_sql { public SqlConnection sqliteConn = null; /// /// 让此类创建一个单例类 /// public static SqliteOptions_sql _instance = null;//申明一个EcgDrawing对象,复制Null private static readonly object lockHelper = new object(); public static SqliteOptions_sql CreateInstance() { if (_instance == null) { lock (lockHelper) { if (_instance == null) _instance = new SqliteOptions_sql(); } } return _instance; } public SqliteOptions_sql() { GetSqliteConnection(); } /// /// 获得sqlite数据库连接 /// /// /// public bool GetSqliteConnection() { try { //给数据库连接类赋值 string ConnectionStr = File.ReadAllText(Application.StartupPath+ @"\conn.dll"); //string ConnectionStr = "server='rds75123g9jf9kk05833public.sqlserver.rds.aliyuncs.com,3433';database='ainia_holter_test';uid='ainia_test';pwd='ain4nhcEqV3gkAP7';"; SqlConnection Conn = new SqlConnection(ConnectionStr); sqliteConn = Conn; } catch { return false; } return true; } /// /// 执行sqlite查询语句,并返回一个 DataTable /// /// /// public DataTable ExcuteSqlite(string sqliteStr) { SqlDataAdapter sqliteDp = new SqlDataAdapter(sqliteStr, sqliteConn); DataSet sqliteds = new DataSet(); try { //if (sqliteConn.State == ConnectionState.Open) // sqliteConn.Close(); sqliteDp.SelectCommand.CommandTimeout = 600000; sqliteDp.Fill(sqliteds); return sqliteds.Tables[0]; } catch { } return new DataTable(); } /// /// Sqlite 添加数据的操作函数 /// /// /// public bool SqliteAdd(string sqliteSQL) { try { if (sqliteConn.State == ConnectionState.Closed) sqliteConn.Open(); SqlCommand sqliteCmd = new SqlCommand(sqliteSQL, sqliteConn); sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); sqliteConn.Close(); } catch { return false; } return true; } /// /// Sqlite 删除数据的操作函数 /// /// /// public bool SqliteDelete(string sqliteSQL) { try { if (sqliteConn.State == ConnectionState.Closed) sqliteConn.Open(); SqlCommand sqliteCmd = new SqlCommand(sqliteSQL, sqliteConn); sqliteCmd.CommandTimeout = 3600; sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); sqliteConn.Close(); } catch { return false; } return true; } /// /// Sqlite 更新数据的操作函数 /// /// /// public bool SqliteUpdate(string sqliteSQL) { try { if (sqliteConn.State == ConnectionState.Closed) sqliteConn.Open(); SqlCommand sqliteCmd = new SqlCommand(sqliteSQL, sqliteConn); sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); sqliteConn.Close(); } catch { return false; } return true; } /// /// 执行SQL语句函数 /// /// SQL语句 /// SQL语句中的参数组 /// 返回受影响记录条数 public int ExecuteSql(string sqlcmd, params SqlParameter[] paras) { SqlCommand cmd = new SqlCommand(sqlcmd, sqliteConn); if (sqliteConn.State == ConnectionState.Closed) { sqliteConn.Open(); } foreach (SqlParameter p in paras) { cmd.Parameters.Add(p); } int cnt = cmd.ExecuteNonQuery(); sqliteConn.Close(); return cnt; } /// /// Sqlite 执行数据库的多条语句, /// /// /// public bool SqliteExecuteNonQuery(string[] sqliteSQL) { if (sqliteConn.State == ConnectionState.Closed) { sqliteConn.Open(); } SqlTransaction sqlTran = sqliteConn.BeginTransaction(); try { foreach (string sql in sqliteSQL) { SqlCommand sqliteCmd = new SqlCommand(sql, sqliteConn, sqlTran); sqliteCmd.ExecuteNonQuery(); sqliteCmd.Dispose(); } sqlTran.Commit(); } catch { sqlTran.Rollback(); return false; } finally { sqliteConn.Close(); } return true; } } }