如何修复此代码以将所有属性带入访问数据库? [英] How to fix this code to bring all properties in access database ?
问题描述
Public Sub ShowFieldsAccess(dgv As DataGridView, table As DevExpress.XtraEditors.CheckedListBoxControl, txtmasar As DevExpress.XtraEditors.TextEdit, txtpk As TextBox)
dgv.DataSource = Nothing
dgv.Rows.Clear()
Dim NameTable As String = table.Text
For Each Dr In mytb.Rows
Dim cmd As OleDbCommand = cn.CreateCommand
cmd.CommandText = String.Format("Select * From [{0}]", NameTable)
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.KeyInfo)
x = reader.GetSchemaTable
Next
For Each item As DataRow In x.Rows
Dim n As Integer = dgv.Rows.Add(item)
dgv.Rows(n).Cells(1).Value = item(0).ToString
dgv.Rows(n).Cells(2).Value = GetTypeAccessGET(item(5).ToString.Replace("System.", ""))
If item(8).ToString() = "True" Then : dgv.Rows(n).Cells(0).Value = "False"
Else : dgv.Rows(n).Cells(0).Value = "True" : End If
n += 1
'-----------------
Dim con As New ADODB.Connection
con.Open(cn.ConnectionString)
Dim MyDB As New ADOX.Catalog
MyDB.ActiveConnection = con
Dim MyTable As ADOX.Table = MyDB.Tables(NameTable)
Dim Desc As String = item(0).ToString
dgv.Rows(n - 1).Cells(3).Value = MyTable.Columns(Desc).Properties("Description").Value
Next item
'--------------------------------------------------------------------------------------------------------------------S
Dim dt As New DataTable With {.TableName = String.Concat("Schema_", NameTable)}
Dim db As AccessDao.Database
Dim TheTableDef As AccessDao.TableDef
Dim Engine As New AccessDao.DBEngine()
db = Engine.OpenDatabase(txtmasar.Text.ToString)
TheTableDef = db.TableDefs(NameTable)
With TheTableDef
For Each f As AccessDao.Field In .Fields
Dim d As AccessDao.DataTypeEnum
Dim FieldType = CType(
System.ComponentModel.TypeDescriptor _
.GetConverter(d) _
.ConvertFromString(f.Type.ToString),
AccessDao.DataTypeEnum)
For Each item1 As DataRow In x.Rows
Dim v As Integer = dgv.Rows.Count - 1
dgv.Rows(v).Cells(4).Value = f.Required
dgv.Rows(v).Cells(5).Value = f.Size
dgv.Rows(v).Cells(6).Value = f.AllowZeroLength
' v += 1
Next item1
Next f
End With
Return
end sub
我有什么尝试过:
我厌倦了这段代码,我试图将Access数据库中表字段的所有属性带到DataGridView
请帮我发现代码中的错误
非常感谢
https://image.ibb.co/byDdVQ/Capture.png [ ^ ]
推荐答案
获取在表(或任何结果集)的定义中,我使用DataReader的GetSchemaTable方法。
OleDbDataReader.GetSchemaTable Method(System.Data.OleDb) [ ^ ]
To get the definition of a table (or any result set), I use the GetSchemaTable method of a DataReader.
OleDbDataReader.GetSchemaTable Method (System.Data.OleDb)[^]
System.Data.DataTable result = null ;
using
(
System.Data.IDataReader dr
=
this.ExecuteReader
(
System.Data.CommandBehavior.SchemaOnly
|
System.Data.CommandBehavior.KeyInfo
,
cmd
)
)
{
schema = dr.GetSchemaTable() ;
dr.Close() ;
}
返回的DataTable的每一行都描述了SQL语句结果的一列。
对于OleDb提供程序,ProviderType是System.Data.OleDb.OleDbType中的值
OleDbType枚举(System.Data.OleDb) [ ^ ]
我将不得不研究为什么它似乎没有为Access中的列提供那些高级属性。
可能你需要
OleDbConnection.GetOleDbSchemaTable Method(Guid,Object [])( System.Data.OleDb) [ ^ ]
但我还没有调查过。
Each row of the returned DataTable describes one column of the SQL statement result.
For an OleDb provider, the ProviderType is a value from System.Data.OleDb.OleDbType
OleDbType Enumeration (System.Data.OleDb)[^]
I'll have to look into why it doesn't appear to have those advanced properties for columns in Access.
It may be that you need
OleDbConnection.GetOleDbSchemaTable Method (Guid, Object[]) (System.Data.OleDb)[^]
but I haven't investigated it yet.
这篇关于如何修复此代码以将所有属性带入访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!