Esempio n. 1
0
        public static bool SaveToDB(string dataSource, string completeQuery)
        {
            using (SqliteConnection con = new SqliteConnection())
            {
                con.ConnectionString = dataSource;
                try { con.Open(); }
                catch (Exception ex)
                {
                    MakeLogErrorStatic(typeof(DBReader), ex);
                    if (con.State.ToString() == "Open")
                    {
                        con.Close();
                        con.Dispose();
                    }

                    return(false);
                }

                // security check and close connection if necessary
                if (!DBSecurity.IsSecureSQLCommand(completeQuery))
                {
                    MakeLogWarningStatic(typeof(DBReader),
                                         "SaveToDB: Prevented forwarding of insecure sql-command: "
                                         + completeQuery);

                    return(false);
                }

                using (SQLiteCommand cmd = new SQLiteCommand(completeQuery, con))
                {
                    cmd.CommandText = completeQuery;

                    SQLiteDataReader rdr = null;
                    try
                    {
                        cmd.ExecuteReader();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Could not execute SQLiteDataReader in SaveToDB: " + ex);
                    }
                    finally
                    {
                        if (rdr != null)
                        {
                            rdr.Close();
                        }
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
        public static bool LoadFromDB(ref List <List <List <object> > > results,
                                      string completeQuery, string dataSource)
        {
            SqliteConnection con = new SqliteConnection();

            con.ConnectionString = dataSource;
            try { con.Open(); }
            catch (Exception ex)
            {
                MakeLogErrorStatic(typeof(DBReader), ex);
                if (con.State.ToString() == "Open")
                {
                    con.Close();
                    con.Dispose();
                }
                return(false);
            }

            // security check and close connection if necessary
            if (!DBSecurity.IsSecureSQLCommand(completeQuery))
            {
                MakeLogWarningStatic(typeof(DBReader),
                                     "LoadFromDB: Prevented forwarding of insecure sql-command: "
                                     + completeQuery);

                return(false);
            }

            using (SQLiteCommand cmd = new SQLiteCommand(completeQuery, con))
            {
                SQLiteDataReader rdr = null;
                try
                {
                    rdr = cmd.ExecuteReader();
                    if (rdr == null)
                    {
                        return(false);
                    }
                    if (!rdr.HasRows)
                    {
                        return(true);
                    }

                    // temporary array to put all data of a row into
                    object[] rowArr = null;

                    do
                    {
                        // add new result-list
                        results.Add(new List <List <object> >());
                        while (rdr.Read())
                        {
                            // create and fill array of the temporary data row
                            rowArr = new object[rdr.FieldCount];
                            rdr.GetValues(rowArr);
                            results[results.Count - 1].Add(new List <object>(rowArr));
                        }
                    }while (rdr.NextResult());
                }
                catch (Exception ex)
                {
                    throw new Exception("LoadFromDB: Could not execute SQLiteDataReader: " + ex);
                }
                finally
                {
                    if (rdr != null)
                    {
                        rdr.Close();
                        rdr.Dispose();
                    }
                }
            }

            // close connection if still opened
            if (con.State.ToString() == "Open")
            {
                con.Close();
                con.Dispose();
            }

            // everything went through without errors thrown
            return(true);
        }