C#如何使用getschema方法获取列的列表 [英] C# how to get list of columns using getschema method
问题描述
大家好!
我正在尝试检索数据库中特定表中的列列表。以下是我到目前为止:
var dtCols = conn.GetSchema(Columns,new [] {waveform_db,null,Electrical}) ;
这是一种正确的方法吗?如果是这样,我如何访问此列表以添加到组合框?以下是我到目前为止的情况。
Hi everyone!
I am trying to retrieve a list of columns in a particular table in the database. Here is what I have so far:
var dtCols = conn.GetSchema("Columns", new[] { "waveform_db", null, "Electrical" });
Is this a proper way? If so, how can I access this list to add to a combobox? Here is what I have so far.
if (dtCols != null)
{
foreach (var dr in dtCols)
{
// code to add to combobox
}
}
编译器在foreach语句的开头就讨厌了。
foreach语句不能对system.data.datatable类型的变量进行操作,因为system.data.datatable不包含getenumerator的公开定义。
我不确定这意味着什么。有没有人对如何解决这个问题有任何暗示?
谢谢大家!
The compiler is balking at the beginning of the foreach statement.
"foreach statement cannot operate on variables of type "system.data.datatable" because "system.data.datatable" does not contain a public definition for "getenumerator".
I''m not real sure what this means. Does anyone have any hints as to how to resolve this issue?
Thanks everyone!
推荐答案
你可以这样做:
You could do it like this:
Try
Dim SQLConnection As SqlConnection = New SqlConnection()
SQLConnection.ConnectionString = DBconnectionWork
Dim SQLCommand As SqlCommand = SQLConnection.CreateCommand()
SQLCommand.CommandText = "select c.name from syscolumns c (nolock), sysobjects o (nolock) " + _
"where c.id = o.id and o.name = 'tablename' " + _
" and c.name <> 'timestamp' " + _
"order by colid"
SQLConnection.Open()
Dim myReader As SqlDataReader = SQLCommand.ExecuteReader()
Do While myReader.Read()
ComboBox1.Items.Add(myReader.GetString(0))
Loop
myReader.Close()
SQLConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical)
End Try
foreach (DataRow dr in dtCols.Rows)
您的具体错误是因为您需要迭代DataTable的行而不是DataTable本身。
但是:
Wietze Bron是正确的,他的方法正是你所需要的。
Your specific error is because you need to iterate the Rows of the DataTable not the DataTable itself.
However:
Wietze Bron is correct, his method is exactly what you need.
这篇关于C#如何使用getschema方法获取列的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!