精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
I have XML file that is written from the Dataset only with ReadSchema attribute. Now I have modified data in XML only and want to update Database with the changes made in Xml.我从数据集编写XML文件,数据集只有ReadSchema属性。现在我已经修改XML内数据,只想把修改数据更新到数据库里。
I have used the transaction as details are given in article but it is not reflecting to database. Is there any way to do the same?我使用了本文中说的事务,但它不反映到数据库。还有其他的办法吗?
I am not sure I understand correctly what you are doing but here's my guess我不确定我有没有正确理解你的意思,但这是我的猜测
You created a dataset from a XML file with ReadSchema attribute. This means that you loaded schema information from the file, but no data您从一个XML文件创建了一个数据集与ReadSchema属性。这意味着您从文件中加载模式信息,但没有数据
You updated the XML file. You need to make changes to the dataset object, not the xml file. My suggestion is that you either read both the schema and data from the XML file (using the ReadXml method) after making changes to the file, or you modify the Dat你更新的XML文件。你需要更改数据集对象,而不是xml文件。我的建议是你可以从XML文件读取模式和数据(使用ReadXml方法)进行更改后文件,或者修改
First I loaded Dataset with original values in Database and saved in XMl file with,首先我加载数据集与原始值在数据库和保存在XMl文件中,
I updated 1 or 2 field's data in XMl file only and saved it.我在XMl文件中更新1或2字段的数据并保存它。
Now, I am reading Schema and Data both from that updated XML file , like现在,我正在从XML文件,读模式和数据,并更新,如下所示:
dataset.ReadXml (filename, XmlReadMode.ReadSchema);
And also Dataset giving me all values as in XMl file. 数据集,也给了我所有的值如XMl文件
Now How can I update Database with this dataset.现在我怎么能使用这个数据集更新数据库。
Right now I am using:现在我使用:
SqlConnection cnn = new SqlConnection(ConnectionString);
cnn.Open();
DataSet d1 = new DataSet();
d1.ReadXml (filename, XmlReadMode.ReadSchema);
SqlTransaction sqlTrans= cnn.BeginTransaction();
string cmdString = "SELECT * FROM " + "Customer";
SqlCommand cmd = new SqlCommand (cmdString, cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.TableMappings.Add("Table", "Customer");
SqlCommandBuilder custCB = new SqlCommandBuilder(da);
custCB.QuotePrefix = "[";
custCB.QuoteSuffix = "]";
// we must specify the transaction used by these adapter.
da.SelectCommand.Transaction= sqlTrans;
d1.AcceptChanges();
// update the database
da.Update (d1, "Customer");
// don't forget to commit
sqlTrans.Commit();
cnn.Close();
But it is not updating Database with the values as in dataset.但作为数据集它不是更新数据库中的值。
What is the problem? 这是什么问题?
he problem is that you called the AcceptChanges method. This method resets the DataRowState property of all the rows (there will be no Added, Modified or Deleted rows anymore). This is why when you call the Update method nothing happens (the dataset belie问题在于AcceptChanges方法。这种方法重置的DataRowState财产的所有行(没有添加,修改或删除行了)。这就是为什么当你调用Update方法(数据集时什么也没有发生
However, you are not using the best approch. Had you not called the AcceptChanges method, all the rows from the DataSet would have been new rows (just loaded from the XML file) and the update method would have tried to insert all the rows in the database.然而,你没有使用最好的方法处理。不要用AcceptChanges方法,所有的行数据集将新行(从XML文件加载)和更新方法将试图插入数据库中。
Here's what you should do. Create two datasets objects, one with original data from the database and the second one that reads from XML file. Then call the Merge method of the first dataset (the one with original values). Then call update on the first data你应该做的是。创建两个数据集对象,一个用数据库初始化,另外一个从XML文件初始化。然后第一个(有原始数据)数据集调用合并方法。然后基于第一个数据调用更新。
Thanks It is done.谢谢你,已经解决了