PublicSharedFunction MakeCreateTableStatements(ByVal ds As DataSet) AsString
Dim result AsString = String.Empty
Dim sb AsNew StringBuilder
ForEach dt As DataTable In ds.Tables
sb.Append(
"CREATE TABLE " + dt.TableName + " (")
sb.Append(MakeCreateTableStatements(dt))
sb.Append(
")")
sb.Append(vbCrLf +
"GO" + vbCrLf)
Next
result = sb.ToString()
Return result
EndFunction
PublicSharedFunction MakeCreateTableStatements(ByVal dt As DataTable) AsString
Dim result AsString = String.Empty
Dim sb AsNew StringBuilder
Dim cnt AsInteger = dt.Columns.Count - 1
Dim i AsInteger = 1
If cnt >= 0 Then sb.Append(MakeCreateTableStatements(dt.Columns(0)))
For i = 1 To cnt
sb.Append(
", ")
sb.Append(MakeCreateTableStatements(dt.Columns(i)))
Next
result = sb.ToString()
Return result
EndFunction
PublicSharedFunction MakeCreateTableStatements(ByVal dc As DataColumn) AsString
Dim result AsString = String.Empty
result = dc.ColumnName +
" " + MakeCreateTableStatements(dc.DataType.ToString()) + " " + IIf(dc.AutoIncrement, ("IDENTITY(" + dc.AutoIncrementSeed.ToString() + "," + dc.AutoIncrementStep.ToString() + ") "), "") + IIf(dc.AllowDBNull, "NULL", "NOT NULL")
Return result
EndFunction
PublicSharedFunction MakeCreateTableStatements(ByVal dtype AsString) AsString
Dim result AsString = String.Empty
SelectCase dtype
Case"System.Boolean"
result =
"bit"
Case"System.Byte"
result =
"tinyint"
Case"System.Int16"
result =
"smallint"
Case"System.Int32"
result =
"int"
Case"System.Int64"
result =
"bigint"
Case"System.SByte"
result =
"smallint"
Case"System.UInt16"
result =
"int"
Case"System.UInt32"
result =
"bigint"
Case"System.UInt64"
result =
"decimal(20)"
Case"System.Decimal"
'should be change for precision or scientific applications
result =
"money"
Case"System.Single"
result =
"float(24)"
Case"System.Double"
result =
"float(53)"
Case"System.String"
If dtype.StartsWith("vl") Then'very long string
result =
"varchar(256)"
ElseIf dtype.StartsWith("ml") Then'medium long string
result =
"varchar(80)"
ElseIf dtype.StartsWith("txt") Then'text field
result =
"text"
Else
result =
"varchar(35)"
EndIf
CaseElse
result = dtype
EndSelect
Return result
EndFunction