【问题标题】:Column 'xxxx' does not belong to underlying table '' error列“xxxx”不属于基础表“”错误
【发布时间】:2017-04-07 21:24:23
【问题描述】:

请指教;这段代码有什么问题? 给出以下错误...列'xxxx'不属于基础表''。

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim connStr As String = ConfigurationManager.ConnectionStrings("FBB-DSL-DBConnectionString").ConnectionString
        Dim tablex As New DataTable()

        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand("sp_columns", conn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@table_name", "tbl_EmpRecords")

                ' get the adapter object and attach the command object to it
                Using ad As New SqlDataAdapter(cmd)
                    ' fire Fill method to fetch the data and fill into DataTable
                    ad.Fill(tablex)
                End Using

            End Using
        End Using

        'Creating DataView
        Dim view As New DataView(tablex)
        Dim dt As DataTable = view.ToTable(False, "COLUMN_NAME" )

        CheckBoxList1.DataSource = dt
        CheckBoxList1.DataBind()
    End If
End Sub

【问题讨论】:

  • view.ToTable 的第二个参数应该是 DataTable 现有列的名称 tablex 你在tablex?
  • 是 Tablex 包含一个名为“xxxx”的列
  • 对不起,我没有其他解释。您的 TableX 使用系统定义的 sp_column 存储过程填充。此存储过程会生成一个没有名为“xxxxx”的字段的数据表。您尝试使用的列的真实名称是什么?
  • 我正在使用 sp_column sp 来提取 tbl_EmpRecords 的列名。结果是 12 列,其中之一称为 EmpUsername 和 EmpGender 。所以我填充了tablex然后需要使用datview过滤结果然后将最终结果分配给cbl。
  • 所以你的 tablex 得到了 12 行。在其中一个名为 COLUMN_NAME 的列下有单词 EmpUserName 而另一行,对于同一列,包含单词 EmpGender 如您所见,您没有在 tableX 数据表中有一个名为 EmpGender 或 _EmpUserName 的列我认为我在下面的回答解释了问题

标签: sql-server vb.net ado.net dataview


【解决方案1】:

系统定义存储过程sp_columns生成一个数据表,其中每一行都包含tbl_EmpRecords中列的架构详细信息。
这意味着此 tablex 中存在的列如下:

TABLE_QUALIFIER, 
TABLE_OWNER,     
TABLE_NAME,      
COLUMN_NAME,     
DATA_TYPE,       
TYPE_NAME,       
PRECISION,       
LENGTH,          
SCALE,           
RADIX, 
NULLABLE, 
SQL_DATA_TYPE, 
SQL_DATETIME_SUB, 
CHAR_OCTET_LENGTH, 
ORDINAL_POSITION, 
IS_NULLABLE, 
SS_DATA_TYPE

每列的含义见MSDN docs about sp_columns

因此,没有名为 XXXXXX 的列,而这正是错误告诉您的内容。如果你真的想创建一个只有一列的新数据表,那么你的 XXXXX 字段应该是上述名称之一。

' This will return a new table with only one column named COLUMN_NAME'
' The table will contain all the column names of the table tbl_EmpRecords'
Dim dt As DataTable = view.ToTable(False, "COLUMN_NAME")

相反,如果您尝试使用该表中定义的字段(IE、empName、HireDate 等...)对 tbl_EmpRecords 进行排序,那么您必须使用不同的方法您的查询。

   ....
   Using conn As New SqlConnection(connStr)
        Using cmd As New SqlCommand("select * from tbl_EmpRecords", conn)
            Using ad As New SqlDataAdapter(cmd)
                ad.Fill(tablex)
            End Using
        End Using
    End Using
    ....

然后您可以继续使用 DataView 代码。
但是,查询数据库以仅检索您感兴趣的数据总是更好。
因此,如果您只需要字段 XXXXX,那么只需使用查询

   select XXXXX from tbl_EmpRecords

也许您可以要求数据库为您排序该记录

   select XXXXX from tbl_EmpRecords ORDER BY XXXXX

删除所有 DataView 代码。

【讨论】:

  • 感谢您的贡献,但目的是提取列名。
  • 添加了一个例子来更好地解释如何提取表的所有列名tbl_EmpRecords
  • 谢谢,我明白你关于 sp_columns 的观点。* 我首先用 sp_coulmns 结果填充 dt .. * 然后使用数据视图选择列'COLUMN_NAME';结果是:COLUMN_NAME TransactionID TransactionDate TransactionAddedBy PerformanceID LoginID VendorID GroupID GradeID 但仍然没有找到提取 2 列(vendorID&GradeID)的方法,我只需要使用 dt.Select 分配给 cbl
  • 我还是不明白你希望在这个 cbl 中看到什么(一个组合框?)你能告诉我这个 cbl 的具体内容是什么吗?
  • 我需要用表格所需的列名填充复选框列表控件,然后我将创建一个网格视图以从复选框列表选项中提取数据。
猜你喜欢
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
  • 2019-07-24
  • 1970-01-01
相关资源
最近更新 更多