[英]VB.NET - Any way to get a nice clean list of all of the column names from a mdb? (Access Database)
我在Access數據庫中有一個表,該表正在加載到VB.NET程序中,並且我想要一種將每個列的名稱獲取到字符串列表中的方法。 谷歌搜索表明,使用SQL數據庫要容易得多,但是在Access中這樣做要困難得多。
我碰到了這個問題是否存在一個查詢,該查詢將返回Microsoft Access表中的所有列名? 這提供了一種方法,但是當我嘗試代碼時,它只是用一堆“ System.Collections.Generic.List'1 [System.String]”填充列表。
這是我改編的代碼。 對修復或更好的方法有什么建議嗎?
Public Function GetColumns(ByRef database As String, ByRef table_name As String, ByRef columns As List(Of String)) As Integer
Dim com As OleDbConnection
Try
com = New OleDbConnection(database)
com.Open()
Catch ex As Exception
Return 1
End Try
Dim restrictions As String() = New String() {Nothing, Nothing, table_name, Nothing}
Dim dt As DataTable = com.GetSchema("Columns", restrictions)
com.Close()
For Each DR As DataRow In dt.Rows
columns.Add(DR("Column_Name").ToString)
Next
For Each holding As String In columns
Console.WriteLine(columns)
Next
End Function
編輯:啊,我討厭當我在函數的其他地方犯了愚蠢的錯誤時,它使我認為函數的內容無法正常工作。 我的每個循環都沒有打印正確的字符串。 應該是Console.WriteLine(holding)
Console.WriteLine(columns)
改成:
Console.WriteLine(holding)
然后可能會踢自己的頭部,以免發現= D ;-)
嘗試改變
For Each holding As String In columns
Console.WriteLine(columns)
Next
至
For Each holding As String In columns
Console.WriteLine(holding)
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.