SqlBulkCopy - 将 XML 数据导入 SQL 表
我在使用 SqlBulkCopy
导入 XML 文件时遇到问题。 XML 文件如下所示:
<root>
<Automobiles>
<Car Id="1" Name="SomeModel1"/>
<Car Id="2" Name="SomeModel2"/>
<Car Id="3" Name="SomeModel2"/>
</Automobiles>
</root>
我的表如下所示:
Int Id
varchar Name
这是我的代码:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("autolist.xml"));
SqlConnection connection = new SqlConnection(
ConfigurationManager.ConnectionStrings["Connection"].ToString());
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "Automobiles";
foreach (DataColumn dc in ds.Tables[0].Columns)
{
sbc.ColumnMappings.Add(dc.Caption, dc.Caption);
}
connection.Open();
sbc.WriteToServer(ds.Tables[0]);
connection.Close();
我只能导入 Id
而不能导入 Name,因为它的属性和 DataSet
仅包含一个柱子。有什么方法可以将属性映射到列吗?
也许使用 XmlDocument
而不是 DataSet
更好?
我想我有什么问题。当我在 Automobiles 元素周围有一些根元素并且数据集表中缺少列时,数据集有很大不同。它在没有根元素的情况下运行良好。
I'm having proplems with importing an XML file using SqlBulkCopy
. The XML file looks like this:
<root>
<Automobiles>
<Car Id="1" Name="SomeModel1"/>
<Car Id="2" Name="SomeModel2"/>
<Car Id="3" Name="SomeModel2"/>
</Automobiles>
</root>
My table looks like this:
Int Id
varchar Name
Here is my code:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("autolist.xml"));
SqlConnection connection = new SqlConnection(
ConfigurationManager.ConnectionStrings["Connection"].ToString());
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "Automobiles";
foreach (DataColumn dc in ds.Tables[0].Columns)
{
sbc.ColumnMappings.Add(dc.Caption, dc.Caption);
}
connection.Open();
sbc.WriteToServer(ds.Tables[0]);
connection.Close();
I could only import the Id
but not Name because its attribute and the DataSet
contains only one column. Is there any way to map the attributes to columns?
Maybe its better to use XmlDocument
instead of DataSet
?
I think i what is wrong. Dataset is quite different when i have some root element around Automobiles element and columns are missing in Dataset tables. It works well without root element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常不喜欢使用
DataSet
将 XML 数据导入到我的数据库中;DataSet
类有一种非常具体的方法将其数据构造为 XML,反之亦然,读取其他 XML。也就是说,我更喜欢调用
WriteToServer
重载 需要一个IDataReader
执行。您可以轻松创建一个采用
IEnumerable
实现并将其映射到
IDataReader
;基本上,您可以通过名称和索引将类的属性映射到表上的字段,其中索引也是Expression
实例(编译为 lambda),您为T
创建,它采用T
的实例和返回属性值。您通常会映射
IDataReader.Read< /code>
实现
IEnumerable.MoveNext
方法。一旦您完成了类似的操作,您就可以将 XML 映射到镜像表的轻量级数据对象,然后将它们的序列传递到扩展方法中,该扩展方法将序列映射到 IDataReader 实现。
I generally don't like using a
DataSet
to import XML data into my database; theDataSet
class has a very specific way of structuring its data as XML and conversely, reading other XML.That said, I prefer call the
WriteToServer
overload that takes anIDataReader
implementation.You can easily create an extension method that takes an
IEnumerable<T>
implementation and maps it to anIDataReader
; basically, you map the properties of the class to the fields on the table by name and index where the index is also an index into an array ofExpression
instances (compiled to lambdas) that you create forT
which takes the instance ofT
and returns the property value.You'd generally map the
IDataReader.Read
implementation toIEnumerable<T>.MoveNext
method.Once you have something like this in place, you can map your XML to a lightwieght data object which mirrors the table and then pass the sequence of them into an extension method which maps the sequence to an
IDataReader
implementation.