【问题标题】:GetSchemaTable() does not return the IsKey propertyGetSchemaTable() 不返回 IsKey 属性
【发布时间】:2018-06-26 12:10:57
【问题描述】:

-你好,世界!-

我正在开发一个带有 ASP.NET 的 C# 项目,但遇到了障碍。该项目是从表中动态加载元数据和记录以对其进行编辑,而无需静态定义可以编辑哪些表。因此,我需要获取不同表的架构/元数据。

这是我目前所拥有的:

// 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 字段始终为空。我的 SQL Server 表是使用以下查询创建的:

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 进行数据库连接,而不是实时服务器。

【问题讨论】:

标签: c# sql asp.net sql-server asp.net-mvc


【解决方案1】:

休斯顿,我们起飞了!

在 mjwills 的帮助下,我设法通过将代码更改为以下内容来使其正常工作:

// 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)
        });
    }
}

感谢那些对我最初的问题发表评论的人的所有帮助:)

【讨论】:

    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 2010-09-25
    • 2022-01-26
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 2015-05-23
    • 2014-03-15
    相关资源
    最近更新 更多