我正在构建一个供我自己使用的工具,除了其他功能之外,我还可以将SQL数据库中的表导入和导出为XML格式。我的公司有一个工具,它已经执行此操作但只允许导入/导出而不是需要添加到其中的其他功能。我有一些由我们的旧工具输出的XML文件的副本,我希望复制它输出的格式。下面就是那种格式。
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ini_files">
<xs:complexType>
<xs:sequence>
<xs:element name="instance" type="xs:int" minOccurs="0" />
<xs:element name="file" type="xs:string" minOccurs="0" />
<xs:element name="section" type="xs:string" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
<xs:element name="value" type="xs:string" minOccurs="0" />
<xs:element name="last_updated" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<ini_files>
<instance>0</instance>
<file>versions.ini</file>
<section>Actual Versions</section>
<key>28.000</key>
<value>28.000</value>
<last_updated>2015-08-25T17:44:10Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>User</section>
<key>CD Key</key>
<value>86BC011090861C115020</value>
<last_updated>2016-02-17T03:32:20Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>Install</section>
<key>Serial Number</key>
<value>492679715G806LS|1570621586</value>
<last_updated>2016-02-19T00:23:40Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>User</section>
<key>Connect</key>
<value>dsconnection</value>
<last_updated>2016-02-17T03:32:20Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>dsconnection</section>
<key>IP Address</key>
<value>127.0.0.1,192.168.0.1,192.168.1.1</value>
<last_updated>2016-02-26T00:26:25Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>dsconnection</section>
<key>Port</key>
<value>8080</value>
<last_updated>2015-08-25T17:44:11Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>dsconnection</section>
<key>Company Type</key>
<value>1</value>
<last_updated>2016-02-17T03:32:08Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>dsconnection</section>
<key>Environment</key>
<value>3Ljf10vSut+g5Pcsu4qwVnjwCN4SEoL8</value>
<last_updated>2016-02-26T00:26:26Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>User</section>
<key>Address</key>
<value>TESTING--ADDRES--VALUE</value>
<last_updated>2016-02-19T00:23:57Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>User</section>
<key>Temporary Address</key>
<value>78577739280119836672551</value>
<last_updated>2016-02-17T03:32:09Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>versions.ini</file>
<section>Profile Versions</section>
<key>Last Modified</key>
<value>2016-02-22T14:22:49</value>
<last_updated>2016-02-22T21:22:49Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>dsconnection.ini</file>
<section>dsconnection</section>
<key>Last Connected</key>
<value>2016-02-25T17:26:36</value>
<last_updated>2016-02-26T00:26:36Z</last_updated>
</ini_files>
<ini_files>
<instance>0</instance>
<file>desktop.ini</file>
<section>Online</section>
<key>Last Sent Info</key>
<value>2016-02-25T01:43:09.3739698Z</value>
<last_updated>2016-02-25T01:43:09Z</last_updated>
</ini_files>
</NewDataSet>
我对SQL很缺乏经验但这里是我发现允许我从表中导出到XML的查询,但格式完全关闭。
osql -S (local)\instancename -U sa -P P@ssW0rd-d db_name -oC:\testing.xml -Q"SELECT * from ini_files for XML AUTO, BINARY BASE64"
如果我不在BINARY BASE64
导出,则会抛出此错误。
FOR XML AUTO requires primary keys to create references for 'data'.
Select primary keys, or use BINARY BASE64 to obtain binary data in
encoded form if no primary keys exist.
我也尝试使用XML RAW
并抛出此错误。
FOR XML EXPLICIT and RAW modes currently do not support addressing
binary data as URLs in column 'data'. Remove the column, or use the
BINARY BASE64 mode, or create the URL directly using the
'dbobject/TABLE[@PK1="V1"]/@COLUMN' syntax.
我做了一些搜索但是却无法找到复制输出的具体内容。
任何人都可以确定用于导出上面列出的XML的命令吗?任何帮助将不胜感激。
答案 0 :(得分:1)
找到我自己的答案。格式是.NET / C#的一部分。这是我用来生成它的代码。
string sExportTableQuery = "SELECT* from ini_files";
sSharedServer = "(local)\\instancename";
sSharedDatabase = "db_name";
sSharedUser = "sa";
sSharedPassword = "P@ssW0rd";
string sConnectionString = "Server=" + sSharedServer + ";Database=" + sSharedDatabase + ";User Id=" + sSharedUser + ";Password=" + sSharedPassword + ";";
using (SqlConnection sqlConnection = new SqlConnection(sConnectionString))
{
SqlCommand sqlCommand = new SqlCommand(sExportTableQuery, sqlConnection);
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
DataSet ds = new DataSet();
da.Fill(ds);
ds.Tables[0].WriteXml(@"C:\text.xml", XmlWriteMode.WriteSchema);
}