This extension method will convert a datatable into an AstTableNode.

The datatype mapping is not complete

Diese extension method wandelt eine DataTable in eine AstTableNode um.

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
	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
		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="">
		<OleDbConnection Name="MyConn" ConnectionString="foo"/>
			<Database Name="MyDB" ConnectionName="MyConn"/>
		<Schema Name="dbo" DatabaseName="MyDB"></Schema>
 <#@ 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="">
	 	<#= SampleDataTable.ToAstTableNode(Schema).GetBiml #>
<#+ 	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")
		Dim colBoolean As DataColumn = New DataColumn("Column_2") 
		colBoolean.DataType = System.Type.GetType("System.Boolean")
		Dim colDateTime As DataColumn = New DataColumn("Column_3") 
		colDateTime.DataType = System.Type.GetType("System.DateTime") 
		Dim colDecimal As DataColumn = New DataColumn("Column_4") 
		colDecimal.DataType = System.Type.GetType("System.Decimal") 
		Dim ColString As DataColumn = New DataColumn("Column_5") 
		ColString.DataType = System.Type.GetType("System.String") 
		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!