首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GetSchemaTable()不返回IsKey属性

GetSchemaTable()不返回IsKey属性
EN

Stack Overflow用户
提问于 2018-06-26 12:10:57
回答 1查看 993关注 0票数 0

-Hello,世界!-

我正在做一个C#和ASP.NET项目,我遇到了一个麻烦。该项目将动态地从表中加载元数据和记录,以编辑这些元数据和记录,而无需静态地定义可以编辑哪些表。因此,我需要获得不同表的模式/元数据。

到目前为止,我的情况如下:

代码语言:javascript
复制
// initialize the connection
using (SqlConnection con = new SqlConnection(metadata.DatabaseString))
{
    // open the connection
    con.Open();

    // initialize a new SqlCommand to get the schema
    SqlCommand command = con.CreateCommand();
    command.CommandType = CommandType.Text;

    // 0 = 1 ensures it's always an empty data set
    command.CommandText = "SELECT * FROM " + metadata.TableName + " WHERE 0=1;";

    // set to SchemaOnly to improve performance (i think)
    SqlDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly);

    // GetSchemaTable() gets the table's metadata
    DataTable dataTable = reader.GetSchemaTable();

    // loops through all the rows of the data table
    foreach (DataRow row in dataTable.Rows)
    {
        // field names found here: https://msdn.microsoft.com/en-us/library/system.data.datatablereader.getschematable(v=vs.110).aspx#Remarks
        metadata.ColumnMetadata.Add(new ColumnWrapper()
        {
            ColumnType = GetTypeFromSql(row.Field<string>("DataTypeName")),
            ColumnRawType = row.Field<string>("DataTypeName"),
            ColumnName = row.Field<string>("ColumnName"),
            ByteSize = row.Field<int>("ColumnSize"),
            IsKey = row.Field<bool?>("IsKey") ?? false
        });
    }
}

问题是IsKey字段总是空的。使用以下查询创建了我的Server表:

代码语言:javascript
复制
CREATE TABLE [dbo].[Dtm_LKUP_Role] (
    [DtmRoleId] INT IDENTITY (1, 1) NOT NULL,
    [RoleName]  VARCHAR(32) NOT NULL,
    [IsActive]  BIT DEFAULT ((1)) NOT NULL,
    PRIMARY KEY CLUSTERED ([DtmRoleId] ASC)
);

以下是我迄今尝试过的:

  • 使用不同的表,结果相同
  • 接入dataTable.Columns["IsKey"]

无论我在哪里看,我都找不到我需要的信息。有谁知道是什么原因造成的吗?如果与此相关,我将使用MDF文件和LocalDB来连接数据库,而不是使用活动服务器。

EN

回答 1

Stack Overflow用户

发布于 2018-06-26 13:08:52

休斯敦,我们起飞了!

基于mjwills的帮助,我将代码更改为:

代码语言:javascript
复制
// initialize the connection
using (SqlConnection con = new SqlConnection(metadata.DatabaseString))
{
    // open the connection
    con.Open();

    // initialize a new SqlCommand to get the schema. 0 = 1 ensures an empty data set
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM " + metadata.TableName + " WHERE 0=1", con);

    // GetSchemaTable() gets the table's metadata
    DataTable dataTable = new DataTable();

    // tell the adapater to fill in the missing schema
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // fill the datatable with the schema
    adapter.FillSchema(dataTable, SchemaType.Mapped);

    // loops through all the rows of the data table
    foreach (DataColumn column in dataTable.Columns)
    {
        // field names found here: https://msdn.microsoft.com/en-us/library/system.data.datatablereader.getschematable(v=vs.110).aspx#Remarks
        metadata.ColumnMetadata.Add(new ColumnWrapper()
        {
            ColumnType = column.DataType,
            ColumnName = column.ColumnName,
            ByteSize = column.MaxLength,
            IsKey = dataTable.PrimaryKey.Contains(column)
        });
    }
}

我很感谢那些对我原来的问题发表评论的人的帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51042631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档