数据库对象
创建数据库对象
C# 应用程序使用 new 操作符来实例化对象,并使用赋值操作符来修改对象的值。要在数据库中存储该对象,需在事务中调用 Connection 方法 Insert() 。例如:
[Persistent] // Class Record will be stored in SmartEDB database
class Record
{
[Indexable(Unique=true)] // Create unique (tree) SmartEDB index by "id" field
public int id;
public String str;
}
...
Connection con = new Connection(db);
con.StartTransaction(Database.TransactionType.ReadWrite); // Start RW transaction
Record rec = new Record();
rec.id = 3;
rec.str = "Record 3";
con.Insert(rec);
con.CommitTransaction();
要更新一个对象,首先使用游标导航找到该对象,然后修改其字段,并使用对象句柄调用游标的 Update() 方法。例如:
cursor = new Cursor<Record>(con, "id");
// find record with id == 3
rec = cursor.Find(3);
rec.str = "Updated string";
cursor.Update();
cursor.Close(); //release cursor
con.CommitTransaction();
要从数据库中删除一个对象,首先使用游标导航找到该对象,然后调用游标的 Remove() 方法并传入对象句柄。例如:
cursor = new Cursor<Record>(con, "id");
// find record with id == 3
rec = cursor.Find(3);
cursor.Remove(); // remove current object (pointed to by the cursor)
cursor.Close(); //release cursor
con.CommitTransaction();
可空字段
在 C# 中,类字段可以通过 [Nullable]
属性显式声明为可空类型,或者使用标准的问号语法(例如:int? i
;)。例如:
class Record
{
[Indexable(Unique=true)] // Create unique (tree) SmartEDB index by "id" field
public int id;
public int? age;
[Nullable]
public int weight;
}
在这里,年龄和体重这两个字段都可能为空值。
有关包含在索引中的可空字段的行为说明,请参阅“可空字段和索引”页面。
字符串与数组
字符串字段的“长度”属性可用于确定其实际的字节大小。另外,由于 SmartEDB 中的向量(又称为动态数组)仅通过使用本地语言的数组语法进行声明,因此该字段的“长度”属性给出向量元素的数量。
在 C# 应用程序中,向量(数组)字段的元素是通过使用常规的数组元素操作符 [] 并指定所需元素的索引来访问的。
二进制大对象支持
C# 应用程序使用 [Blob] 字段属性来定义二进制大对象字段。
日期、时间以及日期时间字段
有关 SmartEDB 日期、时间以及日期时间数据库字段类型的详细说明,请参阅日期时间字段页面。有关确定精度以及访问日期、时间以及日期时间字段的 C# API 的说明,请参阅 C# 中管理日期时间字段页面。