【问题标题】:How to fill a dataset with metadata even if the select query return no rows即使选择查询不返回任何行,如何用元数据填充数据集
【发布时间】:2012-07-26 19:14:57
【问题描述】:

我正在使用 WPF 数据绑定到数据集来显示表格的内容。
SELECT 语句不能返回任何记录,并且数据表不会从中获取任何元数据。然后 WPF DataGrid 被最小化,没有列。
我知道可以专门查询表的元数据,但我正在检查是否有速记方法。

如果查询为空,我该如何获取列信息?

人们喜欢看代码,所以这里是
目前我有:

<DataGrid AutoGenerateColumns="True" EnableRowVirtualization="True" 
    ItemsSource="{Binding Path=SelectedUserTable, Mode=TwoWay}"  Name="userTablesContentGrid"
    RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Stretch">            
</DataGrid>

在我的视图模型中。调用 DisplayUserTable 加载SelectedUserTable 属性

public void DisplayUserTable(string tableName)
{
    if (!UserTablesDataSet.Tables.Contains(tableName))
    {
        FillTable(tableName);
    }
    SelectedUserTable = UserTablesDataSet.Tables[tableName];
    InvokePropertyChanged("SelectedUserTable");
}

private void FillTable(string tableName)
{
    UserTablesDataAdapter.SelectCommand = new SqlCommand(string.Format("SELECT * FROM [{0}]", tableName), _conn);
    UserTablesDataAdapter.Fill(UserTablesDataSet, tableName);
}

【问题讨论】:

    标签: c# wpf ado.net


    【解决方案1】:

    发出 DataAdapter.FillSchema 命令。

    UserTablesDataAdapter.FillSchema(UserTablesDataSet, SchemaType.Mapped);
    

    【讨论】:

    • FillSchema 方法是否替代了 Fill 方法?
    • 不。这只是填充模式。一般来说,每次你需要你想要的东西时,你都想在 Fill 之前发出 FillSchema 方法。
    • 谢谢,但我已经尝试过了,没有任何改变:UserTablesDataAdapter.FillSchema(UserTablesDataSet, SchemaType.Mapped, tableName); UserTablesDataAdapter.Fill(UserTablesDataSet, tableName);
    • 请参考这个 MSDN 示例 - msdn.microsoft.com/en-us/library/ms135707.aspx - 填充没有表名的数据集是您需要做的 - 它只会填充 select 语句中存在的表。
    • 谢谢迈克。毕竟我的数据集发生了变化,但它没有显示在数据网格中。
    猜你喜欢
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多