public string printToConsole(SQLiteDataReader readerDB, int index)
        {
            if (readerDB.IsDBNull(index))
            {
                //return "NULL";
                return "";
            }
            else
            {
                String dataObject = readerDB.GetFieldType(index).ToString();
                switch (dataObject)
                {
                    case "System.Int32":
                        return readerDB.GetInt32(index).ToString();
                    case "System.DateTime":
                        DateTime date = readerDB.GetDateTime(index);
                        return Convert.ToString(date);
                    case "System.String":
                        return readerDB.GetString(index);
                    default:
                        return "Unknown";
                }
            }

        }
Esempio n. 2
0
 public static void ReadValue(SQLiteDataReader reader, int index, TypeStorage type, ICdlValueWriter writer)
 {
     switch (type)
     {
         case TypeStorage.Boolean:
             writer.SetBoolean(reader.GetInt32(index) != 0);
             break;
         case TypeStorage.Byte:
             writer.SetByte((byte) reader.GetInt32(index));
             break;
         case TypeStorage.Int16:
             writer.SetInt16((short) reader.GetInt32(index));
             break;
         case TypeStorage.Int32:
             writer.SetInt32((int) reader.GetInt32(index));
             break;
         case TypeStorage.Int64:
             writer.SetInt64((long) reader.GetInt64(index));
             break;
         case TypeStorage.SByte:
             writer.SetSByte((sbyte) reader.GetInt32(index));
             break;
         case TypeStorage.UInt16:
             writer.SetUInt16((ushort) reader.GetInt32(index));
             break;
         case TypeStorage.UInt32:
             writer.SetUInt32((uint) reader.GetInt32(index));
             break;
         case TypeStorage.UInt64:
             writer.SetUInt64((ulong) reader.GetInt64(index));
             break;
         case TypeStorage.DateTime:
             writer.SetDateTime(DateTime.Parse(reader.GetString(index), CultureInfo.InvariantCulture));
             //writer.SetDateTime(DateTime.ParseExact(reader.GetString(index), "s", CultureInfo.InvariantCulture));
             break;
         case TypeStorage.DateTimeEx:
             writer.SetDateTimeEx(DateTimeEx.ParseNormalized(reader.GetString(index)));
             break;
         case TypeStorage.DateEx:
             writer.SetDateEx(DateEx.ParseNormalized(reader.GetString(index)));
             break;
         case TypeStorage.TimeEx:
             writer.SetTimeEx(TimeEx.ParseNormalized(reader.GetString(index)));
             break;
         case TypeStorage.Decimal:
             {
                 var dtype = reader.GetFieldType(index);
                 decimal value;
                 if (dtype == typeof (string))
                 {
                     value = Decimal.Parse(reader.GetString(index), CultureInfo.InvariantCulture);
                 }
                 else
                 {
                     value = (decimal) reader.GetDouble(index);
                 }
                 writer.SetDecimal(value);
             }
             break;
         case TypeStorage.Float:
             writer.SetFloat((float) reader.GetDouble(index));
             break;
         case TypeStorage.Double:
             writer.SetDouble((double) reader.GetDouble(index));
             break;
         case TypeStorage.String:
             writer.SetString(reader.GetString(index));
             break;
         case TypeStorage.Guid:
             writer.SetGuid(new Guid(reader.GetString(index)));
             break;
         case TypeStorage.ByteArray:
             writer.SetByteArray((byte[]) reader.GetValue(index));
             break;
         case TypeStorage.Null:
             writer.SetNull();
             break;
         default:
             throw new Exception("DBSH-00167 Unsupported field type:" + type.ToString());
     }
 }
Esempio n. 3
0
    public List <T> TableAsList <T>() where T : new()
    {
        var map   = GetMapping(typeof(T));
        var str   = typeof(T).Name;
        var query = "select * from " + str;

#if !RUNINSERVER
        var connection = _connection;
        connection.CommandText = query;
        connection.Prepare();
        var n    = connection.GetColumnCont();
        var list = new List <T>(n);
        try
        {
            var cols = new TableMapping.Column[n];
            for (var i = 0; i < n; i++)
            {
                var name = connection.ColumnName16(i);
                var col  = map.FindColumn(name);
                cols[i] = col;
                if (col == null)
                {
                    Debug.LogError("table:" + str + " ,col:" + name + " is Null");
                    throw new Exception();
                }
            }

            var t = map.MappedType;
            while (connection.Step() == SQLite3.Result.Row)
            {
                var obj = Activator.CreateInstance(t);
                for (var i = 0; i < n; i++)
                {
                    var col = cols[i];
                    var val = connection.ReadCol(i, col.ColumnType);
                    col.FieldInfo.SetValue(obj, val);
                }

                list.Add((T)obj);
            }
        }
        catch (Exception e)
        {
            Debug.LogErrorFormat("{0}\n : {1}", str, e);
        }
        finally
        {
            connection.FinalizeStmt();
        }

        return(list);
#else
        System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
        cmd.Connection  = _connection;
        cmd.CommandText = query;

        //cmd.Prepare();
        Debug.Log("query:" + query);
        System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();

        int n = reader.FieldCount;
        Debug.Log("FieldCount:" + n + " reader.StepCount:" + reader.StepCount);

        //cmd.

        List <T> list = new List <T>(n);

        try
        {
            var cols = new TableMapping.Column[n];
            for (int i = 0; i < cols.Length; i++)
            {
                var name = reader.GetName(i);
                cols[i] = map.FindColumn(name);
                //if (cols[i] == null)
                Debug.LogError("table:" + typeof(T).Name + " ,col:" + name + " " + cols[i].Name);
            }

            //for (int j=0;j<reader.StepCount;j++)
            while (reader.Read())
            {
                //Debug.Log("int read:"+ cols.Length);
                var obj = Activator.CreateInstance(map.MappedType);
                for (int i = 0; i < cols.Length; i++)
                {
                    //if (cols[i] == null)
                    //    continue;
                    var    colType = reader.GetFieldType(i);
                    object val     = reader.GetValue(i);; //  _connection.ReadCol(i, colType, cols[i].ColumnType);
                                                          //Debug.Log("val:" + i + " " + val);
                    if (colType == typeof(double))
                    {
                        //val = 2.5f;//reader.GetFloat(i);
                        float vv = (float)((double)val);
                        cols[i].FieldInfo.SetValue(obj, vv);
                    }
                    else
                    {
                        cols[i].FieldInfo.SetValue(obj, val);
                    }
                }
                list.Add((T)obj);
            }
        }
        catch (Exception e)
        {
            Debug.LogErrorFormat("{0}\n : {1}", typeof(T).Name, e);
        }
        finally
        {
            //_connection.Close();
        }
        return(list);
#endif
    }
Esempio n. 4
-1
		public static QueryRow Read(SQLiteDataReader reader)
		{
			var result = new QueryRow();

			reader.GetValues();

			for (var i = 0; i < reader.FieldCount; i++)
			{
				result.queryDefinition.Fields.Add(
					reader.GetName(i),
					new FieldDefinition(
						reader.GetName(i),
						reader.GetFieldType(i).ToSqlDbType(),
						false,
						false
					)
				);

				result.values.Add(
					reader.GetName(i),
					reader.GetValue(i)
				);
			}

			return result;
		}