在 vb.net 中,如何从数据表中获取列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6791894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In vb.net, how to get the column names from a datatable
提问by hi.im.new
So i create a datatable from a table i can't directly view(i.e. using sql server management). I want to find the column names that are in the datatable, what would be the correct way do this?
所以我从一个我不能直接查看的表中创建了一个数据表(即使用 sql server 管理)。我想找到数据表中的列名,正确的方法是什么?
回答by Brian Webster
This is how to retrieve a Column Name from a DataColumn:
这是从 DataColumn 中检索列名的方法:
MyDataTable.Columns(1).ColumnName
To get the name of all DataColumns within your DataTable:
要获取数据表中所有数据列的名称:
Dim name(DT.Columns.Count) As String
Dim i As Integer = 0
For Each column As DataColumn In DT.Columns
name(i) = column.ColumnName
i += 1
Next
References
参考
回答by Jeffrey Kevin Pry
You can loop through the columns collection of the datatable.
您可以遍历数据表的列集合。
VB
VB
Dim dt As New DataTable()
For Each column As DataColumn In dt.Columns
Console.WriteLine(column.ColumnName)
Next
C#
C#
DataTable dt = new DataTable();
foreach (DataColumn column in dt.Columns)
{
Console.WriteLine(column.ColumnName);
}
Hope this helps!
希望这可以帮助!
回答by LarsTech
Look at
看着
For Each c as DataColumn in dt.Columns
'... = c.ColumnName
Next
or:
或者:
dt.GetDataTableSchema(...)
dt.GetDataTableSchema(...)
回答by Simon Jensen
Do you have access to your database, if so just open it up and look up the column and use an SQL call to retrieve the needed.
您是否有权访问您的数据库,如果可以,只需打开它并查找列并使用 SQL 调用来检索所需的数据。
A short example on a form to retrieve data from a database table:
从数据库表中检索数据的表单的简短示例:
Form contain only a GataGridView named DataGrid
表单只包含一个名为 DataGrid 的 GataGridView
Database name: DB.mdf
数据库名称:DB.mdf
Table name: DBtable
表名:DBtable
Column names in table: Name as varchar(50), Age as int, Gender as bit.
表中的列名:名称为 varchar(50),年龄为 int,性别为位。
Private Sub DatabaseTest_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Public ConString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\{username}\documents\visual studio 2010\Projects\Userapplication prototype v1.0\Userapplication prototype v1.0\Database\DB.mdf;" & "Integrated Security=True;User Instance=True"
Dim conn As New SqlClient.SqlConnection
Dim cmd As New SqlClient.SqlCommand
Dim da As New SqlClient.SqlDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
Try
conn = New SqlClient.SqlConnection(ConString)
conn.Open() 'connects to the database
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "SELECT * FROM DBtable" 'Sql to be executed
cmd.CommandText = sSQL 'makes the string a command
da.SelectCommand = cmd 'puts the command into the sqlDataAdapter
da.Fill(dt) 'populates the dataTable by performing the command above
Me.DataGrid.DataSource = dt 'Updates the grid using the populated dataTable
'the following is only if any errors happen:
If dt.Rows.Count = 0 Then
MsgBox("No record found!")
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close() 'closes the connection again so it can be accessed by other users or programs
End Try
End Sub
This will fetch all the rows and columns from your database table for review.
If you want to only fetch the names just change the sql call with: "SELECT Name FROM DBtable" this way the DataGridView will only show the column names.
这将从您的数据库表中获取所有行和列以供查看。
如果您只想获取名称,只需使用以下方式更改 sql 调用:“SELECT Name FROM DBtable” 这样 DataGridView 将仅显示列名称。
I'm only a rookie but i would strongly advise to get rid of theses auto generate wizards. Using SQL you have full access to your database and what happens.
Also one last thing, if your database doesn't use SQLClient just change it to OleDB.
我只是一个新手,但我强烈建议摆脱这些自动生成向导。使用 SQL,您可以完全访问您的数据库以及会发生什么。
还有最后一件事,如果您的数据库不使用 SQLClient,只需将其更改为 OleDB。
Example: "Dim conn As New SqlClient.SqlConnection" becomes: Dim conn As New OleDb.OleDbConnection
示例:“ Dim conn As New SqlClient.SqlConnection”变为:Dim conn As New OleDb.OleDbConnection
回答by Kay Kay
' i modify the code for Datatable
For Each c as DataColumn in dt.Columns
For j=0 To _dataTable.Columns.Count-1
xlWorksheet.Cells (i+1, j+1) = _dataTable.Columns(j).ColumnName
Next
Next
Hope this could be help!
希望这会有所帮助!

