Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create SQL Server DB from DataSet

I read xsd and xml file in DataSet, now I want create db from this DataSet

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 (in example use varchar(max)). How to fix this?

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 need script to create db tables with size of columns (like in xsd)

UPD: Maybe use XmlSchemaSet to parse XSD?

like image 806
e1s Avatar asked Jun 01 '15 11:06

e1s


People also ask

How do I create a database from an existing SQL Server database?

In SQL Server Object Explorer, under the SQL Server node, expand your connected server instance. Right-click the Databases node and select Add New Database. Rename the new database to TradeDev. Right-click the Trade database in SQL Server Object Explorer, and select Schema Compare.

How do I create a database from an existing MDF file?

Click in the text area and type a Create Database statement using the following Transact-SQL code as a guide: CREATE DATABASE MyDatabase ON (FILENAME = 'c:\data files\my_data. mdf'), (FILENAME = ' c:\data files\my_data.


1 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

like image 59
Imran Ali Khan Avatar answered Sep 23 '22 00:09

Imran Ali Khan