Create SQL Server Database from DataSet
I am reading xsd and xml file in DataSet
, now i want to create db from thisDataSet
foreach (DataTable dt in temp.Tables) {
foreach (DataColumn dc in dt.Columns) {
//example for one column
SqlCommand createtable = new SqlCommand(
"create table " + dt.TableName + " ("
+ dc.ColumnName + " varchar(max))", conn);
createtable.ExecuteNonQuery();
}
}
But I have some problem when I create db table I need column type and size from XSD (use in example varchar(max)
). How to fix it?
For example, in xsd I have
<xs:restriction base="xs:string">
<xs:maxLength value="36"/>
<xs:minLength value="1"/>
</xs:restriction>
or
<xs:restriction base="xs:string">
<xs:maxLength value="40"/>
</xs:restriction>
or
<xs:restriction base="xs:decimal">
<xs:totalDigits value="19"/>
<xs:fractionDigits value="2"/>
</xs:restriction>
In the end, I will need a script to create db tables with column sizes (like xsd)
UPD: Can it be used XmlSchemaSet
to parse XSD?
source to share
I think there is no general solution to this. XSD
can easily describe something that doesn't map to a relational database. While you can try to "automate" this, your XSD must be designed with a relational database, or it won't work.
However, you can move your data in memory to sql server database with SQLServer Management Objects
and SqlBulkCopy
.
For more information refer to this link. Hope this helps you.
source to share