本文介绍了如何创建从多个表的SQL表中的XML / XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从多个表的SQL表



我想要做的就是创建一个数据库(或数据库架构)的XML架构

有一些有趣的代码



尤其

  DataSet的结果=新的DataSet (); 

的SqlCommand命令=新的SqlCommand(SELECT * FROM表,新的SqlConnection(的connectionString));

SqlDataAdapter的sqlAdapter =新SqlDataAdapter的(命令);

sqlAdapter.FillSchema(结果,SchemaType.Mapped); //填充数据集从查询
results.WriteXmlSchema(MYSCHEMA)架构;



但我怎么生成多个表中的这个XML模式?如果我只是做

  SELECT * FROM表1,表2 

所有数据都在一个大的blob在XML



我要的是在XML中定义的每个表与它分开的内容。
的东西。


解决方案

您可以单独抓住每个表的模式,并使用一个XDocument相结合。下面就来抓住从数据库查询架构功能:

 的XDocument GrabSchema(
串数据集名称,
SQL字符串)
{
变种CON =新的SqlConnection(<< YourConnectionString>>);
VAR命令=新的SqlCommand(SQL,CON);
变种sqlAdapter =新SqlDataAdapter的(命令);
var数据=新的DataSet(数据集名称);
sqlAdapter.FillSchema(DataSet中,SchemaType.Mapped);
变种流=新的MemoryStream();
dataSet.WriteXmlSchema(流);
stream.Seek(0,System.IO.SeekOrigin.Begin);
返回XDocument.Load(XmlReader.Create(流));
}

您再结合这两个表模式,如:

  VAR firstSchema = GrabDataset(T1,选择= 49); 
VAR secondSchema = GrabDataset(T2,选择B ='50');
firstSchema.Root.Add(secondSchema.Root.Elements());

$ B:

您可以验证相结合的模式将软件加载到其他数据集是有道理的
$ b

 数据集的数据集=新的DataSet(); 
dataSet.ReadXmlSchema(firstSchema.CreateReader());


How to create XML/XSD from SQL table with multiple tables

what I want to do is create an XML schema from a database (or database schema)

This post has some interesting code

Particularly

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

But how do I generate this XML schema from multiple tables? If I just do

Select * from table1,table2

All of the data is in one big blob in the XML

What I want is each table defined in the XML with it's contents separately. something

解决方案

You could grab the schema for each table separately, and combine it using an XDocument. Here's a function to grab a schema from a database query:

XDocument GrabSchema(
   string dataSetName,
   string sql)
{
   var con = new SqlConnection("<<YourConnectionString>>");
   var command = new SqlCommand(sql, con);
   var sqlAdapter = new SqlDataAdapter(command);
   var dataSet = new DataSet(dataSetName);
   sqlAdapter.FillSchema(dataSet, SchemaType.Mapped);
   var stream = new MemoryStream();
   dataSet.WriteXmlSchema(stream);
   stream.Seek(0, System.IO.SeekOrigin.Begin);
   return XDocument.Load(XmlReader.Create(stream));
}

You then combine the two table schemas like:

var firstSchema = GrabDataset("t1", "select a = 49");
var secondSchema = GrabDataset("t2", "select b = '50'");
firstSchema.Root.Add(secondSchema.Root.Elements());

You can verify the combined schema makes sense by loading it into another DataSet:

DataSet dataSet = new DataSet();
dataSet.ReadXmlSchema(firstSchema.CreateReader());

这篇关于如何创建从多个表的SQL表中的XML / XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:06