Example #1
0
        private static string Query(string query)
        {
            var report = "";

            if(MODE == DBMODE.SQLITE) {
                //then actually do it

                if(currentDatabaseName.Length > 0) {

                    var conn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", currentDatabaseName));
                    conn.Open();

                    IDataReader results = null;
                    string errorText = string.Empty;

                    try{

                        results = new SQLiteCommand(query, conn).ExecuteReader();

                    }catch(Exception e) {

                        errorText = e.Message;
                    }

                    if(results != null) {

                        //i must keep the same order. i could do dict int, dict name type... but meah
                        var columns = new string[results.FieldCount];
                        var columnTypes = new Type[results.FieldCount];

                        for(int i=0; i < results.FieldCount; ++i) {
                            columns[i] = results.GetName(i);
                            columnTypes[i] = results[columns[i]].GetType();
                        }

                        DataTable table = null;

                        while(results.Read()) {

                            if(table == null) {
                                table = new DataTable(columns, columnTypes);}

                            var row = new DataRow();

                            foreach(string columnName in columns) {
                                row[columnName] = results[columnName];}

                            if(table != null) {
                                table.AddRow(row);}
                        }

                        if(table != null){
                            report = table.ToString();
                        }else{
                            report = results.RecordsAffected + " rows affected";}

                    }else{
                        report = errorText;}

                    conn.Close();

                }else{

                    report = "you need to select a database before you can execute a command. use \\u your_database_name";
                }

            }else{

                report = "currently only supporting sqlite mode...";
            }

            return report;
        }