C# - 将数据集插入 SQL 表
我有一个 DataSet,用 XML 文件中的值填充。我想将这些值插入到 SQL 表中。我该怎么做?
这是我填充数据集的方式:
DataSet dataset = new DataSet();
dataset.ReadXml(xmlfile);
customer.DataSource = dataset.Tables[0];
I have a DataSet that I fill with values from a XML-file. I would like to insert the values then into a SQL table. How do I do that?
Here is how I fill my DataSet:
DataSet dataset = new DataSet();
dataset.ReadXml(xmlfile);
customer.DataSource = dataset.Tables[0];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您要做的只是插入,那么最简单的方法就是循环遍历
DataTable
中的行,并为每行创建并执行一个DbCommand
。使用的具体语法取决于您使用的数据库类型;对于 SQL Server,它可能看起来像这样:这掩盖了很多很多可能的复杂因素,例如异常处理、过滤掉具有错误
DataRowState
的行、调用AcceptChanges
DataRow
,使用数据库分配的值更新标识列,等等。 ADO.NET 中有很多东西需要理解,这些东西存在其中是有原因的。但是,如果您要做的只是插入行,并且DataTable
中的DataColumn
对象具有正确的名称和类型以及正确的Size 和
AllowDbNull
,并且不涉及外键关系,并且您不会遇到表中现有数据重复的主键,以上应该可以工作。If all you're ever going to do is inserts, then the simplest way is to just loop through the rows in the
DataTable
and create and execute aDbCommand
for each row. The specific syntax to use depends on the kind of database you're using; for SQL Server, it might look like this:This glosses over many, many possible complicating factors, e.g. exception handling, filtering out rows with the wrong
DataRowState
, callingAcceptChanges
on theDataRow
, updating identity columns with the value assigned by the database, and so on. There's a lot to understand in ADO.NET, and that stuff's in there for a reason. But if all you're going to do is insert rows, and theDataColumn
objects in yourDataTable
have the right names and types and proper values forSize
andAllowDbNull
, and there are no foreign-key relations involved, and you're not going to encounter duplicate primary keys with existing data in the table, the above should work.我的建议是为插入创建一个存储过程,然后创建一个遍历数据集的方法,设置存储过程的参数,然后执行它。
My suggestion would be to create a stored procedure for your insert and then create a method that traverses the data set, and sets the parameters of your stored proc and then executes it.
我认为您需要循环遍历数据集并执行插入。
显然,您可以使用存储过程来完成此操作,只需传递参数即可,或者使用 LINQ。
无论哪种方式都应该有效。
I think you would need to loop through the dataset and perform inserts.
You can obviously do this with a sproc, and you just pass along the parameters, or with LINQ.
Either way should work.
这完全取决于您现有的数据访问层。
如果不存在,我会更倾向于 LinqtoSQL 而不是使用存储过程,存储过程更难管理
This completely depends on you existing Data access Layer.
If one doesn't exist i would look more towards LinqtoSQL rather than using Stored procedures, Stored procedures are more difficult to manage
很简单,
只需使用 DataSet.ReadXml() 方法即可。
http://msdn.microsoft.com/en-us /library/system.data.dataset.readxml.aspx
现在,如果您的 xml 与架构不同,那么您很可能必须循环并填充特定于架构的数据集,然后根据需要保存。
Simple,
Just use the DataSet.ReadXml() method.
http://msdn.microsoft.com/en-us/library/system.data.dataset.readxml.aspx
Now if your xml is different from your schema, then your most likely going to have to loop and fill your schema specific dataset, then save as needed.