private static object ReadValue(SqliteDataReader reader, int index, Type col_type) { if (reader.IsDBNull(index)) { return(null); } if (col_type == typeof(string)) { return(reader.GetString(index)); } if (col_type == typeof(int)) { return(reader.GetInt32(index)); } if (col_type == typeof(bool)) { return(reader.GetInt32(index) == 1); } if (col_type == typeof(float)) { return((float)reader.GetDouble(index)); } if (col_type == typeof(double)) { return(reader.GetDouble(index)); } if (col_type == typeof(decimal)) { return((decimal)reader.GetDouble(index)); } if (col_type == typeof(long)) { return(reader.GetInt64(index)); } if (col_type == typeof(uint)) { return((uint)reader.GetInt64(index)); } if (col_type == typeof(byte)) { return((byte)reader.GetInt32(index)); } if (col_type == typeof(ushort)) { return((ushort)reader.GetInt32(index)); } if (col_type == typeof(short)) { return((short)reader.GetInt32(index)); } if (col_type == typeof(sbyte)) { return((sbyte)reader.GetInt32(index)); } if (col_type.IsEnum) { return(reader.GetInt32(index)); } if (col_type == typeof(Guid)) { return(new Guid(reader.GetString(index))); } if (col_type == typeof(byte[])) { var blob = new byte[reader.GetBytes(index, 0, null, 0, int.MaxValue)]; reader.GetBytes(index, 0, blob, 0, blob.Length); return(blob); } if (col_type == typeof(TimeSpan)) { return(new TimeSpan(reader.GetInt64(index))); } if (col_type == typeof(DateTime)) { return(DateTime.Parse(reader.GetString(index))); } if (col_type == typeof(DateTimeOffset)) { return(new TimeSpan(reader.GetInt64(index))); } //数据扩展支持(Array/List/Dict) var normal_fullnam = SupportExtend.GetNormalFullName(col_type); if (SupportExtend.IsSupportType(normal_fullnam)) { var text = reader.GetString(index); return(SupportExtend.ToObject(text, normal_fullnam)); } //自定义接口实现 if (SupportExtend.IsSupportIDataText(col_type)) { var text = reader.GetString(index); var obj = (IDataText)Activator.CreateInstance(col_type); obj.Load(text); return(obj); } if (SupportExtend.IsSupportIDataBytes(col_type)) { var blob = new byte[reader.GetBytes(index, 0, null, 0, int.MaxValue)]; reader.GetBytes(index, 0, blob, 0, blob.Length); var obj = (IDataBytes)Activator.CreateInstance(col_type); obj.Load(blob); return(obj); } throw new NotSupportedException("Don't know how to read " + col_type); }
public static int BindParameter(SqliteCommand command, object value) { //SqliteParameter(DbType dbType, object value)构造函数可正常使用 //SqliteParameter(DbType dbType, string value)将不能插入数值 ///我也不知道为什么会这样 (笑) if (value == null) { return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)""))); } if (value is int) { return(command.Parameters.Add(new SqliteParameter(DbType.Int32, value))); } if (value is string) { return(command.Parameters.Add(new SqliteParameter(DbType.String, value))); } if (value is byte || value is ushort || value is sbyte || value is short) { return(command.Parameters.Add(new SqliteParameter(DbType.Int32, value))); } if (value is bool) { return(command.Parameters.Add(new SqliteParameter(DbType.Int32, (object)((bool)value ? 1 : 0)))); } if (value is uint || value is long) { return(command.Parameters.Add(new SqliteParameter(DbType.Int64, value))); } if (value is float || value is double || value is decimal) { return(command.Parameters.Add(new SqliteParameter(DbType.Double, value))); } if (value.GetType().IsEnum) { return(command.Parameters.Add(new SqliteParameter(DbType.Int64, (object)Convert.ToInt32(value)))); } if (value is byte[]) { return(command.Parameters.Add(new SqliteParameter(DbType.Binary, value))); } if (value is Guid) { return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)((Guid)value).ToString()))); } if (value is TimeSpan) { return(command.Parameters.Add(new SqliteParameter(DbType.Int64, value))); } if (value is DateTime) { return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss")))); } if (value is DateTimeOffset) { return(command.Parameters.Add(new SqliteParameter(DbType.Int64, value))); } //数据扩展支持(Array/List/Dict) var normal_fullnam = SupportExtend.GetNormalFullName(value.GetType()); if (SupportExtend.IsSupportType(normal_fullnam)) { return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)SupportExtend.ToString(value, normal_fullnam)))); } //自定义接口实现 if (SupportExtend.IsSupportIDataText(value.GetType())) { return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)((IDataText)value).Save()))); } if (SupportExtend.IsSupportIDataBytes(value.GetType())) { return(command.Parameters.Add(new SqliteParameter(DbType.Binary, (object)((IDataBytes)value).Save()))); } throw new NotSupportedException("Cannot store type: " + value.GetType()); }