元数据¶
提供两种用于检索 ADO.NET 中元数据的 API。 一个检索有关查询结果的元数据, 另一个检索有关数据库架构的元数据。
查询结果元数据¶
可以使用 OscarDataReader 上的 GetSchemaTable 方法检索有关查询结果的元数据, 返回的 DataTable 包含以下列:
| 列 | 类型 | 描述 |
|---|---|---|
| AllowDBNull | Boolean | 如果原点列可以为 NULL,则为 True。 |
| BaseCatalogName | String | 原点列的数据库的名称。 表达式始终为 NULL。 |
| BaseColumnName | String | 原点列的真实名称。 表达式始终为 NULL。 |
| BaseSchemaName | String | 原点列的模式名称。 |
| BaseTableName | String | 原点列的表的名称。 表达式始终为 NULL。 |
| ColumnName | String | 结果集中列的名称或别名。 |
| ColumnOrdinal | Int32 | 结果集中列的序号。 |
| ColumnSize | Int32 | 原点列的大小。 |
| DataType | Type | 列的默认 .NET 数据类型。 |
| DataTypeName | String | 列的 OscarDbType 数据类型。 |
| IsAliased | Boolean | 如果列名在结果集中具有别名,则为 True。 |
| IsAutoIncrement | Boolean | 如果原点列是使用 AUTO_INCREMENT 关键字创建的,则为 True。 |
| IsExpression | Boolean | 始终为 False。在将来的 Oscar.Data.SqlClient 版本中这可能会发生变化。 |
| IsKey | Boolean | 如果原点列是主键的一部分,则为 True。 |
| IsUnique | Boolean | 如果原点列是唯一的,则为 True。 |
| NumericPrecision | Int32 | 列数据的精度。 |
| NumericScale | Int32 | 列数据的范围。 |
| ProviderType | String | 后台提供的类型名。 |
| IsRowVersion | Boolean | 始终为 False。在将来的 Oscar.Data.SqlClient 版本中这可能会发生变化。 |
| IsIdentity | Boolean | 始终为 False。在将来的 Oscar.Data.SqlClient 版本中这可能会发生变化。 |
| IsHidden | Boolean | 始终为 False。在将来的 Oscar.Data.SqlClient 版本中这可能会发生变化。 |
| IsLong | Boolean | 始终为 False。在将来的 Oscar.Data.SqlClient 版本中这可能会发生变化。 |
| IsReadOnly | Boolean | 始终为 False。在将来的 Oscar.Data.SqlClient 版本中这可能会发生变化。 |
代码示例
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
using System.Data.Common;
using System.Data.OscarClient;
namespace UnitTest
{
[TestClass]
public class SchemaTest : TestInternal
{
private IDbConnection _connect = null;
[TestInitialize]
public void Initialize()
{
_connect = new OscarConnection(DatabaseConnectionStrings);
_connect.Open();
}
[TestCleanup]
public void Dispose()
{
_connect.Dispose();
}
[TestMethod]
public void TestNumeric()
{
using (OscarCommand command = (OscarCommand)_connect.CreateCommand())
{
try
{
command.CommandText = "CREATE TABLE DOTNET_TAB_TEST_NUMBERICS (ID INT, NUM1 DECIMAL,NUM2 DECIMAL(28,10), NUM3 NUMERIC, NUM4 NUMERIC(28,10))";
command.ExecuteNonQuery();
command.CommandText = "SELECT * FROM DOTNET_TAB_TEST_NUMBERICS";
using (var reader = command.ExecuteReader())
{
var schemas = reader.GetColumnSchema();
Assert.AreEqual(18, schemas[1].NumericPrecision);
Assert.AreEqual(0, schemas[1].NumericScale);
Assert.AreEqual(28, schemas[2].NumericPrecision);
Assert.AreEqual(10, schemas[2].NumericScale);
Assert.AreEqual(18, schemas[3].NumericPrecision);
Assert.AreEqual(0, schemas[3].NumericScale);
Assert.AreEqual(28, schemas[4].NumericPrecision);
Assert.AreEqual(10, schemas[4].NumericScale);
}
}
finally
{
command.CommandText = "DROP TABLE DOTNET_TAB_TEST_NUMBERICS";
command.ExecuteNonQuery();
}
}
}
[TestMethod]
public void TestFloat()
{
using (OscarCommand command = (OscarCommand)_connect.CreateCommand())
{
try
{
command.CommandText = "CREATE TABLE DOTNET_TAB_TEST_FLOATS (ID INT, NUM1 REAL,NUM2 DOUBLE PRECISION, NUM3 FLOAT(25))";
command.ExecuteNonQuery();
command.CommandText = "SELECT * FROM DOTNET_TAB_TEST_FLOATS";
using (var reader = command.ExecuteReader())
{
var schemas = reader.GetColumnSchema();
Assert.AreEqual(0, schemas[1].NumericPrecision);
Assert.AreEqual(0, schemas[1].NumericScale);
Assert.AreEqual(0, schemas[2].NumericPrecision);
Assert.AreEqual(0, schemas[2].NumericScale);
Assert.AreEqual(25, schemas[3].NumericPrecision);
Assert.AreEqual(0, schemas[3].NumericScale);
}
}
finally
{
command.CommandText = "DROP TABLE DOTNET_TAB_TEST_FLOATS";
command.ExecuteNonQuery();
}
}
}
[TestMethod]
public void TestMethod()
{
var metadata = ((OscarConnection)_connect).GetSchema();
Assert.AreNotEqual(metadata, null);
}
}
}
架构元数据¶
神通数据库 .NET Data Provider 在 OscarConnection 上实现 GetSchema 方法, 返回当前连接的数据源的架构信息(DataTable).
/// <summary>
/// 返回此 Connection 的数据源的架构信息
/// </summary>
public override DataTable GetSchema()
/// <summary>
/// 根据collection name返回此 Connection 的数据源的架构信息。
/// </summary>
/// <param name="collectionName"指定要返回的架构的名称。</param>
/// <returns>包含架构信息的 DataTable。</returns>
public DataTable GetSchema(string collectionName)
允许的架构名称
| NO | 架构的名称 |
|---|---|
| 1 | MetaDataCollections |
| 2 | Restrictions |
| 3 | DataSourceInformation |
| 4 | DataTypes |
| 5 | ReservedWords |
| 6 | Databases |
| 7 | Tables |
| 8 | Columns |
| 9 | Views |
| 10 | Users |
| 11 | Procedures |
| 12 | ProcedureParameters |
| 13 | Indexes |
| 14 | IndexColumns |
| 15 | ForeignKeys |