Getting XML Schema from MS SQL Database

谁说胖子不能爱 提交于 2019-11-27 13:09:49

问题


Is it possible to generate a XML Schema of a Database programatically with .Net and C#? I want to look into NDbUnit but for big databases it would not really be feasible to make a Schema manually?


回答1:


Is it possible to generate a XML Schema from a Database?

It sure is, XMLSpy can generate XML Schema from a database.

There's another way, though I've never tested it:

create table Person
(
Age int not NULL check( Age > 0) ,
Height numeric(10,2) not NULL check( Height > 5),
Gender varchar(5) not null check( Gender in ('M', 'F', 'O')),
BirthDate datetime null,
)

DECLARE @schema xml
SET @schema = (SELECT * FROM Person FOR XML AUTO, ELEMENTS, XMLSCHEMA('PersonSchema'))
select @schema



回答2:


I could do it like this:

DataSet results = new DataSet();

SqlCommand command = new SqlCommand("SELECT * FROM table", new SqlConnection(connectionString));

SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);

sqlAdapter.FillSchema(results, SchemaType.Mapped);//Fills dataset with schema from query
results.WriteXmlSchema(mySchema);

Problem is, how could I adapt this method so that it could be used with a indeterminate amount of tables? Without building up a string though concatenation which is not exactly ideal!




回答3:


Use MyGeneration's typed-dataset-template to auto-generate the XSD (http://www.mygenrationssoftware.com). Its free, OSS, and works great. Also, the NDbUnit team is presently working on an add-on tool to NDbUnit that will make it possible to not only extract the schema from the database, but also to edit the test data that is contained within the accompanying XML dataset. ETA for this tool is about mid-July.



来源:https://stackoverflow.com/questions/298660/getting-xml-schema-from-ms-sql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!