数据查询
查询地理数据库表
查询geodatabase对象的三种常见的接口是IQueryFilter、ISpatialFilter、IQueryDef接口。每个接口都有不同的功能,如下表所示:
Requirement | IQueryFilter | ISpatialFilter | IQueryDef |
---|---|---|---|
Apply attribute constraints(支持属性约束) | True | True | True |
Apply spatial constraints (支持空间约束) | False | True | False |
Query results contain fields from multiple tables (查询结果包含多个表的字段,可理解为支持多表连接查询) | False | False | True |
Query results returned as a cursor (查询结果返回一个游标对象) | True | True | True |
RecordSet objects can be created from results (从结果可以创建记录集对象) | True | True | True |
Returned records can be edited (查询结果可以被编辑) | True | True | False |
Records include edits made in active edit session | True | True | False |
IQueryFilter接口
属性及方法详解
- SubFields
使用技巧:查询的时候只需设置需要的字段即可,比如你要查询age>15的数据,设置成SubFields=“age”就好了,这样在查询上效率会有所提高,如果不设置该属性,默认查询所有字段(相当于是“*”);如果需要返回全部字段采用默认值即可,不要把SubFields设置为”*”和”“。 - WhereClause
使用技巧:查询条件的语法取决于你使用的数据源,应用程序可以使用ISQLSyntax接口在一个工作空间来确定使用的SQL语法信息,如用于限定表和字段名称和标识引用字符的划界字符。
参考链接:传送门
相关参考资料:待上传……
注意事项:不支持Distinct关键字
相关接口
- IQueryFilterDefinition (可用此接口进行Group By和Order By操作)
- ISQLSyntax
示例代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
使用小窍门
- 查询游标True和False的区别
- True
循环使用对象,查询速度快,只存储一份对象 - False
不循环使用对象,查询速度慢,存储多份对象
- True
- Fields设置技巧
只设置需要查询的字段,这样比不设置和设置*查询速度会快很多 - WhereClause使用技巧
优先使用大于、小于,其次才是不等于
ISpatialFilter接口
属性及方法详解
- Geometry
查询要素的的空间过滤条件(几何对象),设置的Geometry对象必须实现IRelationalOperator接口,如高级别的几何对象(points, polylines, polygons, and geometry bags),该属性不支持低级别的几何对象(lines, paths and rings)。 - GeometryField
查询要素类的几何字段
-SpatialRel
要素类和查询图形的几何关系,具体设置参考官方文档
- 其它参数
请参考帮助文档
相关接口
相关接口同IQueryFilter
示例代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
使用小窍门
If a geometry bag is being used as the filter’s query geometry, create a spatial index for the geometry bag before being assigned to the geometry property. This can drastically increase the query’s efficiency. The following code example shows how to do this:
如果使用几何包对象作为过滤的几何图形,在为ISpatialFilter对象设置Geometry属性之前,先对几何包对象创建空间索引,这样能大大提高查询的效率,下面是创建空间索引的示例代码:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
IQueryDef2接口
IQueryDef对象可由IFeatureWorkspace.CreateQueryDef 方法创建
注意事项:
1)只能在ArcSDE、PGDB、FGDB数据源上使用(注意:shp数据不支持),数据集的历史表示 不被QueryDef游标支持(翻译可能不太准确,详见官方文档)。
2)支持多表查询,但是查询的结果不能进行修改!
属性及方法详解
- Tables
查询的表名称,支持多表查询,用逗号隔开即可,如”TableUser,TableRole”**
注意事项:如果使用的是SDE数据源,当当前的工作空间不是表所在的用户空间下,需要在表的前边加用户名前缀(如:Owner.MyTable) - SubFields
查询的字段列表,默认不填为查询所有字段,即”*”,支持Distinct关键字,
格式:
1) “*” 返回所有字段
2)”field1,field2,field3” 用逗号隔开要查询的字段
3)”table1.*,table2.field1,table3.field2” 查询table1的所有字段,table2的字段1,和table3的字段2 - WhereClause
这块注意不同数据源,查询的SQL写法不同 - PrefixClause
前缀查询条件,在Select 和 Select Column List之间,如Distinct关键字和ALL关键字 - PrefixClause
后缀查询条件,在Select语句后,紧跟Where语句之后,如Order By - Evaluate
返回ICursor对象,这块使用的过程中记得用ComReleaser管理,在使用完毕释放Com对象。
相关接口
IQueryName2
同创建游标一样,IQueryDef对象能用于生成一个虚拟的表或要素类。示例代码如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
示例代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
执行空间查询
参考官方教材:传送门
数据表排序
使用ITableSort接口对有ObjectIDs的表(或要素类)进行排序。使用该接口进行排序,必须设置Fields属性和Table(或SelectionSet)属性。
ITableSort接口
- 参数
可写 | Ascending | |
---|---|---|
可写 | CaseSensitive | CaseSensitive Character fields case sensitive. Default: False. |
可写 | Compare | Compare call back interface. Specify Null (default) for normal behavior. |
可写 | Cursor | The cursor of the data to sort on. Ensure that sorting fields are available. Cancels SelectionSet. |
可写 | Fields | Comma list of field names to sort on. |
可读 | IDByIndex | A id by its index value. |
可读 | IDs | IDs List of sorted IDs. |
可读可写 | QueryFilter | The query filter on table or selection set. |
可读 | Rows | Cursor of sorted rows. |
可读可写 | SelectionSet | The selection set as a source of the data to sort on. Cancels Cursor. |
方法 | Sort | Sort rows. |
可写 | SortCharacters | Number of characters to sort on, for string fields. A null (default) sorts on the whole string. |
可读可写 | Table | The table as a source of the data to sort on. |
- 示例代码
1.排序必须设置的属性
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
排序的几种组合方式
- Table 对表进行排序
- Table + Cusror 使用游标对表进行排序
- Table + QueryFilter 对使用过滤器的表进行排序
- SelectionSet 对选择集进行排序
- SelectionSet + QueryFilter 是使用过滤器的选择集进行排序
示例代码
- 1
- 2
- 3
- 1
- 2
- 3
设置排序的其它参数
- Ascending 升序
- CaseSensitive 只对文本字段有效,大小写敏感(默认为False,大小写不敏感)
- SortCharacters 只对文本字段有效,按指定的字符进行排序(默认为空)
示例代码
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
执行排序
- 1
- 2
- 1
- 2
访问排序的结果
- 使用Rows属性读取排序后的结果
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 使用IDs属性读取排序后的结果
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
自定义排序
自定义类实现ITableSortCallBack接口,实现自己的Compare方法即可进行自定义排序。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
连接数据
两种连接数据,关联查询的方法:
- 使用IQueryDef接口进行关联查询
- 使用RelQueryTables 进行关联查询
示例代码:
- 1
- 1
数据操作
对数据进行插入、更新、删除时最好是开始事务编辑,这样当遇到异常时不会损坏数据,而且也可以进行回滚操作。
数据编辑模式
IWorkspaceEdit接口
StartEditing (boolwithUndoRedo):开启编辑流程(恢复/取消恢复)
StartEditOperation:开始编辑操作
StopEditOperation:结束编辑操作,用来确保编辑操作的完成
StopEditing (boolsaveEdits):结束编辑流程(保存编辑结束编辑流程或不保存编辑结束编辑流程)
UndoEditOperation:用于编辑状态的回滚操作。
RedoEditOperation:用于编辑状态的恢复操作。
AbortEditOperation:取消所有的编辑操作。
数据插入
数据插入有两种方式,一种通过游标进行插入,一种通过创建新行来插入。
示例代码
- 表插入记录
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 要素类插入记录
- 1
- 2
- 3
- 1
- 2
- 3
使用技巧
大量插入数据的时候,用游标插入更快,可以每几千条Flush一次,而不是每插入一条就Flush一次。
数据更新
数据更新有两种方式,一种通过游标进行更新,一种是直接更新。
示例代码
游标更新
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
直接更新
- 1
- 2
- 3
- 1
- 2
- 3
使用技巧
在更新数据的时候,建议开始编辑模式,以防数据编辑失败,还有就是大量数据更新,建议用游标进行更新,使用游标的时候
数据删除
数据删除有好几种方式,1)通过执行SQL进行删除 2)通过更新游标删除 3)直接查询删除 4)直接删除查询的行
示例代码
略
请参考:http://blog.sina.com.cn/s/blog_5e4c933d010116n5.html
使用技巧
删除的时候一定要注意开启编辑模式