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?

+3


source to share


3 answers


use XSD2DB

XSD2DB is a command line tool written in C#

that will read a Microsoft ADO.NET compatible DataSet Schema File (XSD)

and generate a database.



link

+4


source


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.

+2


source


Will scaffolding help you in this situation?

Maybe take a look at this, I know there is a way to reverse engineer the database first with scaffolding

+2


source







All Articles