【问题标题】:Get table name LINQtoSQL获取表名 LINQtoSQL
【发布时间】:2014-10-23 19:32:42
【问题描述】:

我有一个包含数据库中所有表列表的组合框:

using(DataContext db = new DataContext())
{
    cmbbx_tables.DataSource = db.Mapping.GetTables().Select(q=>new {q.TableName, q.RowType}).ToArray();
    cmbbx_tables.DisplayMember = "TableName";
    cmbbx_tables.ValueMember = "RowType";        
}

SelectionChangeCommitted事件上我想获取选中表的名字:

private void cmbbx_tables_SelectionChangeCommitted(object sender, EventArgs e)
{
    using(DataContext db = new DataContext())
    {
        String table = db.Mapping.GetTable((???)cmbbx_tables.SelectedValue).TableName;                
    }
}

我应该如何输入 cast cmbbx_tables.SelectedValue ?

【问题讨论】:

  • 如果你最终需要 String 类型,那你为什么还要进行类型转换?只需cmbbx_tables.SelectedValue 就可以完成这项工作,不是吗? IOW - String table = cmbbx_tables.SelectedValue;
  • 没有。组合框 ValueMember 是 RowType
  • 不确定您的意思。从您的代码String table = ... 和您的问题“我想获得所选表的名称”看来,cmbbx_tables.SelectedValue.ToString() 可以完成这项工作。
  • 我找到了解决方案:(cmbbx_tables.SelectedValue as System.Data.Linq.Mapping.MetaType).Type

标签: c# .net linq-to-sql datacontext


【解决方案1】:

解决办法是:

String name = db.Mapping.GetTable((cmbbx_tables.SelectedValue as System.Data.Linq.Mapping.MetaType).Type).TableName;

【讨论】:

  • 仍然不知道为什么要麻烦地在上下文中调用 GetTable,键入强制转换,然后调用 TableName,当它通过 cmbbx_tables.SelectedValue 或 @987654323 以字符串形式出现在你的膝上时@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
相关资源
最近更新 更多