Esempio n. 1
0
        public void UsingSafeConnection(string sqlStr, Action <SQLiteDataReader> callBack)
        {
            var sql = new SQLiteString(sqlStr);

            using (SQLiteConnection con = new SQLiteConnection(DataSource))
            {
                con.Open();
                using (SQLiteCommand com = new SQLiteCommand(con))
                {
                    try
                    {
                        com.CommandText = sql.SqlText;
                        sql.SetCommand(com);
                        var reader = com.ExecuteReader();
                        callBack(reader);
                        reader.Close();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("SQLite database [{0}] occurse errors", DataSource), ex);
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                    }
                }
            }
        }
Esempio n. 2
0
        public void Find(string sql, Action <dynamic> callback)
        {
            try
            {
                var s = new SQLiteString(sql);

                using (SQLiteConnection con = new SQLiteConnection(DataSource))
                {
                    con.Open();

                    SQLiteCommand com = new SQLiteCommand(con);
                    s.SetCommand(com);

                    using (var reader = com.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            callback(reader.ToDynamic());
                        }
                        reader.Close();
                    }
                    con.Close();
                }
            }
            catch
            {
            }
        }
Esempio n. 3
0
        public void UsingSafeConnection(string sqlStr, Action <DBDataReader> callBack)
        {
            var sql = new SQLiteString(sqlStr);

            using (SQLiteConnection con = new SQLiteConnection(DataSource))
            {
                con.Open();
                using (SQLiteCommand com = new SQLiteCommand(con))
                {
                    try
                    {
                        com.CommandText = sql.SqlText;
                        sql.SetCommand(com);
                        var reader = com.ExecuteReader();
                        callBack(new DBDataReader(reader));
                        reader.Close();
                    }
                    catch (Exception ex)
                    {
                        Log4NetService.LoggerManagerSingle.Instance.Error(ex, string.Format("SQLite database [{0}] occurse errors", DataSource));
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 执行sql,返回首行首列的值
        /// </summary>
        public string ExecuteScalar(SQLiteString sql)
        {
            string result = string.Empty;

            UsingSafeConnection(com =>
            {
                com.CommandText = sql.SqlText;
                sql.SetCommand(com);
                result = com.ExecuteScalar().ToSafeString();
            });
            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// 执行Sql,返回受影响的行数
        /// </summary>
        public int ExecuteNonQuery(SQLiteString sql)
        {
            int result = 0;

            UsingSafeConnection(com =>
            {
                com.CommandText = sql.SqlText;
                sql.SetCommand(com);
                result = com.ExecuteNonQuery();
            });

            return(result);
        }
Esempio n. 6
0
 public void UsingExistingConnection(SQLiteString sql, Action <SQLiteDataReader> callBack)
 {
     using (SQLiteCommand com = new SQLiteCommand(ExistingConnection))
     {
         try
         {
             com.CommandText = sql.SqlText;
             sql.SetCommand(com);
             var reader = com.ExecuteReader();
             callBack(reader);
         }
         catch (Exception ex)
         {
             throw new Exception(string.Format("SQLite database [{0}] occurse errors", DataSource), ex);
         }
     }
 }