Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GetSchemaTable()不返回IsKey属性_C#_Sql_Asp.net_Sql Server_Asp.net Mvc - Fatal编程技术网

C# GetSchemaTable()不返回IsKey属性

C# GetSchemaTable()不返回IsKey属性,c#,sql,asp.net,sql-server,asp.net-mvc,C#,Sql,Asp.net,Sql Server,Asp.net Mvc,-你好,世界- 我正在做一个C#with ASP.NET项目,遇到了一个障碍。该项目是从表中动态加载元数据和记录以进行编辑,而无需静态定义可以编辑的表。因此,我需要获取不同表的模式/元数据 以下是我目前掌握的情况: // initialize the connection using (SqlConnection con = new SqlConnection(metadata.DatabaseString)) { // open the connection con.Open(

-你好,世界-

我正在做一个C#with ASP.NET项目,遇到了一个障碍。该项目是从表中动态加载元数据和记录以进行编辑,而无需静态定义可以编辑的表。因此,我需要获取不同表的模式/元数据

以下是我目前掌握的情况:

// 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
        });
    }
}
以下是我迄今为止所尝试的:

  • 使用不同的表,得到相同的结果
  • 访问
    dataTable.Columns[“IsKey”]

无论我往哪里看,我都找不到我需要的信息。有人知道是什么导致了这一切吗?如果相关,我将使用MDF文件和LocalDB进行数据库连接,而不是使用实时服务器。

休斯顿,我们起飞了

基于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)
        });
    }
}
我感谢那些对我最初的问题发表评论的人的帮助:)

有帮助吗?或者?可能的副本
// 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)
        });
    }
}