This extension method will convert a datatable into an AstTableNode.
Remarks:
The datatype mapping is not complete
Diese extension method wandelt eine DataTable in eine AstTableNode um.
Hinweise:
Das Datatype-Mapping enthält nicht alle Datentypen.
Imports Varigence.Biml.Extensions Imports Varigence.Languages.Biml Imports Varigence.Languages.Biml.Table Imports System.Data Imports System.Runtime.CompilerServices Module DatatableExtensions <Extension()> Public Function ToAstTableNode(DT As Datatable,schema As AstSchemaNode) As AstTableNode Dim BimlTable As New AstTableNode(Nothing) BimlTable.Name = DT.TableName BimlTable.Schema = schema For Each col As DataColumn In dt.Columns Dim tableColumn As New AstTableColumnNode(Nothing) tableColumn.Name = col.ColumnName tableColumn.DataType = GetDbType(col.DataType) tablecolumn.IsNullable = col.AllowDBNull If col.maxlength > -1 Then tableColumn.Length = col.maxlength If tableColumn.DataType = DbType.Decimal Then ' DataColumn doesn't know - so we need some defaults... tablecolumn.Precision = 32 tablecolumn.Scale = 16 End If BimlTable.Columns.Add(tableColumn) Next Return BimlTable End Function Public Function GetDbType(ByVal myType As System.Type) As DbType Select Case myType Case System.Type.GetType("System.Int32") Return dbtype.Int32 Case System.Type.GetType("System.Int16") Return dbtype.int16 Case System.Type.GetType("System.Boolean") Return dbtype.boolean Case System.Type.GetType("System.DateTime") Return dbtype.Datetime Case System.Type.GetType("System.Decimal") Return dbtype.Decimal Case Else 'Add further mappings as needed! Return Nothing End Select End Function End Module
<Biml xmlns="http://schemas.varigence.com/biml.xsd"> <Connections> <OleDbConnection Name="MyConn" ConnectionString="foo"/> </Connections> <Databases> <Database Name="MyDB" ConnectionName="MyConn"/> </Databases> <Schemas> <Schema Name="dbo" DatabaseName="MyDB"></Schema> </Schemas> </Biml>
<#@ template language="VB" tier="2" #> <#@ code file="..\Code\DataTableExtensions.vb" #> <#@ import namespace="System.Data" #> <# Dim Schema as AstSchemaNode if rootnode.schemas.count > 0 then schema = rootnode.schemas(0) #> <Biml xmlns="http://schemas.varigence.com/biml.xsd"> <Tables> <#= SampleDataTable.ToAstTableNode(Schema).GetBiml #> </Tables> </Biml> <#+ Public Function SampleDataTable() As DataTable Dim DT As DataTable DT = New DataTable("Sampletable") Dim colInt32 As DataColumn = New DataColumn("Column_1") colInt32.DataType = System.Type.GetType("System.Int32") DT.Columns.Add(colInt32) Dim colBoolean As DataColumn = New DataColumn("Column_2") colBoolean.DataType = System.Type.GetType("System.Boolean") DT.Columns.Add(colBoolean) Dim colDateTime As DataColumn = New DataColumn("Column_3") colDateTime.DataType = System.Type.GetType("System.DateTime") DT.Columns.Add(colDateTime) Dim colDecimal As DataColumn = New DataColumn("Column_4") colDecimal.DataType = System.Type.GetType("System.Decimal") DT.Columns.Add(colDecimal) Dim ColString As DataColumn = New DataColumn("Column_5") ColString.DataType = System.Type.GetType("System.String") ColString.maxlength=400 DT.Columns.Add(ColString) Return DT End Function #>
Any questions or comments? We’d love to hear from you at !
Further Information on Biml in English can be found on our English Biml Page.
Happy Biml’ing!
Haben Sie hierzu Fragen oder Anmerkungen? Wir freuen uns auf Ihren Input unter !
Weitere Informationen zu Biml, einschließlich Terminen und Blog Beiträgen finden Sie auch auf unserer Biml Seite.
Viel Spaß beim Biml’n!
[…] Ben Weissman has a quick post in which he shows how to convert a .NET DataTable into a Biml AstTable…: […]