从DataSet创建SQL Server数据库

时间:2022-03-13 15:40:35

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

我在DataSet中读取xsd和xml文件,现在我想从这个DataSet创建db

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?

但是我有一些问题,当我创建db表时,我需要XSD中的列类型和大小(例如使用varchar(max))。如何解决这个问题?

For example in xsd I have

例如在我有的xsd中

<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)

最后我需要脚本来创建列大小的db表(比如在xsd中)

UPD: Maybe use XmlSchemaSet to parse XSD?

UPD:也许使用XmlSchemaSet来解析XSD?

3 个解决方案

#1


4  

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.

XSD2DB是一个用C#编写的命令行工具,它将读取与Microsoft ADO.NET兼容的DataSet架构文件(XSD)并生成数据库。

link

#2


2  

Would scaffolding be able to help you in this situation?

脚手架能否在这种情况下帮助你?

Maybe take a look at that, I know there was a way of doing a reverse engeneering of the database first with scaffolding

也许看一下,我知道有一种方法是首先使用脚手架对数据库进行反向编译

#3


2  

I guess there's no general solution to this. An XSD can easily describe something that does not map to a relational database.While you can try to "automate" this, your XSD's must be designed with a relational database in mind, or it won't work out well.

我想这没有一般解决方案。 XSD可以很容易地描述一些不会映射到关系数据库的东西。虽然你可以尝试“自动化”这个,但是你的XSD必须考虑到关系数据库,否则它将无法正常工作。

However you can transfer your datatables in memory to sql server database using SQLServer Management Objects and SqlBulkCopy.

但是,您可以使用SQLServer管理对象和SqlBulkCopy将内存中的数据表传输到sql server数据库。

Please refer to this link for more information. Hope this may help you.

有关更多信息,请参阅此链接。希望这可以帮到你。

#1


4  

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.

XSD2DB是一个用C#编写的命令行工具,它将读取与Microsoft ADO.NET兼容的DataSet架构文件(XSD)并生成数据库。

link

#2


2  

Would scaffolding be able to help you in this situation?

脚手架能否在这种情况下帮助你?

Maybe take a look at that, I know there was a way of doing a reverse engeneering of the database first with scaffolding

也许看一下,我知道有一种方法是首先使用脚手架对数据库进行反向编译

#3


2  

I guess there's no general solution to this. An XSD can easily describe something that does not map to a relational database.While you can try to "automate" this, your XSD's must be designed with a relational database in mind, or it won't work out well.

我想这没有一般解决方案。 XSD可以很容易地描述一些不会映射到关系数据库的东西。虽然你可以尝试“自动化”这个,但是你的XSD必须考虑到关系数据库,否则它将无法正常工作。

However you can transfer your datatables in memory to sql server database using SQLServer Management Objects and SqlBulkCopy.

但是,您可以使用SQLServer管理对象和SqlBulkCopy将内存中的数据表传输到sql server数据库。

Please refer to this link for more information. Hope this may help you.

有关更多信息,请参阅此链接。希望这可以帮到你。