[英]Create SQL Server DB from DataSet
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)
). 但是我有一些问题,当我创建db表时,我需要XSD的列类型和大小(例如使用
varchar(max)
)。 How to fix this? 如何解决这个问题?
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) 最后我需要脚本来创建列大小的数据库表(如在xsd中)
UPD: Maybe use XmlSchemaSet
to parse XSD? UPD:也许使用
XmlSchemaSet
来解析XSD?
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 Management Objects
和SqlBulkCopy
内存中的数据表传输到sql server数据库。
Please refer to this link for more information. 有关更多信息,请参阅此链接。 Hope this may help you.
希望这可以帮到你。
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 也许看一下,我知道有一种方法可以先用脚手架对数据库进行反向编译
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.