首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

VB.net得到数据库字段名 的有关问题

2012-02-19 
VB.net得到数据库字段名 的问题VS2005VB.netAccessWindowsForm请教如何得到某表的全部字段,并将其保存为一

VB.net得到数据库字段名 的问题
VS2005   VB.net   Access   Windows   Form

请教   如何得到某表的全部字段,并将其保存为一个数组,最好有代码,给分

谢谢   !

[解决办法]
构造sql查询数据库的字典表
ref:http://topic.csdn.net/t/20020713/01/870869.html#
oracle与access的类似,或则在dataset的schema中的到对应的列的信息
[解决办法]
用 select top1 * from xxxx 把表的结构保存到一个datatable里,然后从这个datatable里取出列名的信息
[解决办法]
Private Sub GetColName()
Dim myTable As DataTable
Dim columnName() As String
Dim myDs As DataSet
Dim strSql As String= "Select * From Table "
Dim myCon As New SqlConnection(.................)
Dim myAdapter As New SqlDataAdapter(strSql,myCon)
myCon.Open()
myAdapter.Fill(myDs)
myTable=myDs.Tables(0)
Dim myColumn As DataColumn
Dim i As Short=0
For Each myColumn in myTable.Columns
columnName(i)=myColumn.ColumnName)
i+=1
Next
End Sub
[解决办法]
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & "D:\Test.mdb "
Dim strSQL As String = "Select * From TableName "
Dim da As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(strSQL, ConnectionString)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Dim arr(dt.Columns.Count - 1) As String
Dim i As Integer
For i = 1 To dt.Columns.Count
arr(i - 1) = dt.Columns(i - 1).ColumnName
Next
[解决办法]
Dim rs As Recordset

Dim f As Field

For Each f In rs.Fields
Debug.Print f.Name
Next
[解决办法]
vs2005下:

Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & "D:\Test.mdb "
Dim strSQL As String = "Select * From TableName "
Dim da As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(strSQL, ConnectionString)
Dim ds As System.Data.DataSet = New System.Data.DataSet
Dim iCount As Integer
Dim i As Integer
da.Fill(ds, "TableName ")
iCount = ds.Tables( "TableName ").Columns.Count
Dim arr(iCount - 1) As String
For i = 0 To iCount - 1
arr(i) = ds.Tables( "TableName ").Columns(i).ColumnName
Next
End Sub

热点排行