gpt4 book ai didi

.net - System.Data.SQLite 如何处理 .NET 数据类型?

转载 作者:IT王子 更新时间:2023-10-29 06:26:12 25 4
gpt4 key购买 nike

我正在努力寻找有关 System.Data.SQLite 在各种 .NET 数据类型方面的行为的文档。

例如,System.Data.SQLite 如何在 SQLite 数据库中存储 .NET bool 值?有几种可能的方法:

  • 整数 01
  • 整数 0–1
  • 文本 'True''False'
  • 文本 'T''F'
  • 文本 'Y''N'
  • 等...

反之亦然—— bool 值是如何从 SQLite 中解析出来的? System.Data.SQLite 是否需要某种格式?那是什么格式?

缺乏相关文档令人沮丧。也许我没找对地方?

注意:这不是专门针对 bool 值的问题。我正在寻找说明所有 .NET 数据类型的行为的文档

最佳答案

我建议您从与驱动程序无关的 SQLite documentation on the subject 开始.它解释了 bool 值的存储方式,以及不同的日期时间序列化方案,例如。

有关更多详细信息,System.Data.SQLite 是开源的,虽然在某些方面有些粗糙,但通常很容易阅读。

例如,SQLiteDataReader.cs 中的 GetValue() 方法(已实现的 ADO.NET IDataReader 接口(interface)的一部分)调用名为 GetSQLiteType() 的方法,然后根据某些连接标志进行更多的自动检测。

GetSQLiteType() 和 friend 一起回到SQLiteConvert类,它进行实际的类型转换和检测。转换都在那里定义(大约在中途开始,在大量日期操作助手之后)。最终您将获得与您的问题特别相关的功能:

internal static TypeAffinity TypeToAffinity(Type typ)
{
TypeCode tc = Type.GetTypeCode(typ);
if (tc == TypeCode.Object)
{
if (typ == typeof(byte[]) || typ == typeof(Guid))
return TypeAffinity.Blob;
else
return TypeAffinity.Text;
}
return _typecodeAffinities[(int)tc];
}

private static TypeAffinity[] _typecodeAffinities = {
TypeAffinity.Null, // Empty (0)
TypeAffinity.Blob, // Object (1)
TypeAffinity.Null, // DBNull (2)
TypeAffinity.Int64, // Boolean (3)
TypeAffinity.Int64, // Char (4)
TypeAffinity.Int64, // SByte (5)
TypeAffinity.Int64, // Byte (6)
TypeAffinity.Int64, // Int16 (7)
TypeAffinity.Int64, // UInt16 (8)
TypeAffinity.Int64, // Int32 (9)
TypeAffinity.Int64, // UInt32 (10)
TypeAffinity.Int64, // Int64 (11)
TypeAffinity.Int64, // UInt64 (12)
TypeAffinity.Double, // Single (13)
TypeAffinity.Double, // Double (14)
TypeAffinity.Double, // Decimal (15)
TypeAffinity.DateTime, // DateTime (16)
TypeAffinity.Null, // ?? (17)
TypeAffinity.Text // String (18)
};

一般而言,整数类型将正确映射到 SQLite(64 位)整数并返回,字符串也是如此。 byte[] 数组和 Guid 也将透明地工作,尽管它们都存储为 blob。 bool 值映射到 1 (true) 和 0 (false) 整数。并且支持所有 SQLite 日期时间表示,以及更多:请参阅 SQLite3.cs 中的 Bind_DateTime() 方法.

关于.net - System.Data.SQLite 如何处理 .NET 数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com