将数据集写入SQL表

编程入门 行业动态 更新时间:2024-10-18 18:24:02
本文介绍了将数据集写入SQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我已经将一个复杂的XML文件转换为具有7个表和大约70列的数据集我已经在SQL Server中创建了使用XML模式的数据集中的表。如何轻松地将数据集写入SQL表?

解决方案

根据DataSet中有多少行,大概是最好的办法是使用SqlCommandBuilder,如下所示:

var connection = new SqlConnection(my connection string) ; connection.Open(); //对数据集中的每个表重复 var adapterForTable1 = new SqlDataAdapter(select * from table1,connection); var builderForTable1 = new SqlCommandBuilder(adapterForTable1); adapterForTable1.Update(myDataSet,Table1);

如果您在DataSet中的表之间定义了复杂的关系,恐怕你不能使用SqlCommandBuilder。相反,您需要做的是为DataSet中的每个表定义一个数据适配器。然后按照依次顺序更新DataSet中的表(即先行不依赖的表,然后依赖表)。

以下是父/子插入的示例(请注意,您将对更新做类似的事情)。 Table1是父级,并具有ParentId(标识列)和一个NVARCHAR字段ParentValue。 Table2是child,有自己的标识列(ChildId),外键字段(ParentId)和它自己的值(ChildValue)。

var myDataSet = new DataSet(); // **填充数据集的详细信息省略** //创建Table1和Table2之间的外键关系。 //向Table2的ParentId列添加一个约束,表示它必须是 //存在于Table1中。 var fk = new ForeignKeyConstraint(fk,myDataSet.Tables [Table1]。列[ParentId],myDataSet.Tables [Table2]。列[ParentId]) { DeleteRule = Rule.Cascade, UpdateRule = Rule.Cascade }; myDataSet.Tables [Table2]。Constraints.Add(fk); myDataSet.EnforceConstraints = true; var connection = new SqlConnection(我的连接字符串); var adapterForTable1 = new SqlDataAdapter(); adapterForTable1.InsertCommand = new SqlCommand(INSERT INTO MasterTable(ParentValue)VALUES(@ParentValue); SELECT SCOPE_IDENTITY()AS ParentId,connection); adapterForTable1.InsertCommand.Parameters.Add(@ ParentValue,SqlDbType.NVarChar).SourceColumn =ParentValue; var adapterForTable2 = new SqlDataAdapter(); adapterForTable2.InsertCommand = new SqlCommand(INSERT INTO ChildTable(ParentId,ChildValue)VALUES(@ParentId,@ChildValue); SELECT SCOPE_IDENTITY()AS ChildId,connection); adapterForTable2.InsertCommand.Parameters.Add(@ ParentId,SqlDbType.Int).SourceColumn =ParentId; adapterForTable2.InsertCommand.Parameters.Add(@ ChildValue,SqlDbType.NVarChar).SourceColumn =ChildValue; connection.Open(); adapterForTable1.Update(myDataSet,Table1); //在父元首插入行 adapterForTable2.Update(myDataSet,Table2); // child second

I have converted a complex XML File to a Dataset that has 7 Tables and around 70 Columns I have created the tables in SQL Server to match the ones in the Dataset using the XML Schema. How can I easily write my Dataset to the SQL tables?

解决方案

Depending on how many rows you have in the DataSet, probably the best thing to do would be to use a SqlCommandBuilder, like so:

var connection = new SqlConnection("my connection string"); connection.Open(); // repeat for each table in data set var adapterForTable1 = new SqlDataAdapter("select * from table1", connection); var builderForTable1 = new SqlCommandBuilder(adapterForTable1); adapterForTable1.Update(myDataSet, "Table1");

If you have complex relationships defined between the tables in the DataSet, I'm afraid you can't use the SqlCommandBuilder. What you'll need to do, instead, is define a data adapter for each table in your DataSet. Then, update the tables in the DataSet in dependency order (i.e., do the tables with no dependencies first, then the dependent tables).

Here's an example of a parent/child insert (note that you would do similar things for updates). Table1 is the parent, and has ParentId (the identity column), and an NVARCHAR field ParentValue. Table2 is the child, has its own identity column (ChildId), the foreign key field (ParentId), and its own value (ChildValue).

var myDataSet = new DataSet(); // ** details of populating the dataset omitted ** // create a foreign key relationship between Table1 and Table2. // add a constraint to Table2's ParentId column, indicating it must // existing in Table1. var fk = new ForeignKeyConstraint("fk", myDataSet.Tables["Table1"].Columns["ParentId"], myDataSet.Tables["Table2"].Columns["ParentId"]) { DeleteRule = Rule.Cascade, UpdateRule = Rule.Cascade }; myDataSet.Tables["Table2"].Constraints.Add(fk); myDataSet.EnforceConstraints = true; var connection = new SqlConnection("my connection string"); var adapterForTable1 = new SqlDataAdapter(); adapterForTable1.InsertCommand = new SqlCommand("INSERT INTO MasterTable (ParentValue) VALUES (@ParentValue); SELECT SCOPE_IDENTITY() AS ParentId", connection); adapterForTable1.InsertCommand.Parameters.Add("@ParentValue", SqlDbType.NVarChar).SourceColumn = "ParentValue"; var adapterForTable2 = new SqlDataAdapter(); adapterForTable2.InsertCommand = new SqlCommand("INSERT INTO ChildTable (ParentId, ChildValue) VALUES (@ParentId, @ChildValue); SELECT SCOPE_IDENTITY() AS ChildId", connection); adapterForTable2.InsertCommand.Parameters.Add("@ParentId", SqlDbType.Int).SourceColumn = "ParentId"; adapterForTable2.InsertCommand.Parameters.Add("@ChildValue", SqlDbType.NVarChar).SourceColumn = "ChildValue"; connection.Open(); adapterForTable1.Update(myDataSet, "Table1"); // insert rows in parent first adapterForTable2.Update(myDataSet, "Table2"); // child second

更多推荐

将数据集写入SQL表

本文发布于:2023-10-26 18:40:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!