[英]Insert rows on a existing dataset
I have a dataset called "titulos" and have 1 table there called "tb" with the columns with the name "titulo","titulo 2" and "titulo3". 我有一个名为“ titulos”的数据集,并且有1个表名为“ tb”的表,其名称分别为“ titulo”,“ titulo 2”和“ titulo3”。 I'm trying to do an insertion of rows in the event onclick of a button but for some reason my code doesn't work!
我正在尝试在按钮的onclick事件中插入行,但是由于某些原因,我的代码无法正常工作! My dataset is on a xsd file and I am using visual studio 2013 with c#.
我的数据集在xsd文件上,并且我将Visual Studio 2013与c#一起使用。 I already tried this code but I don't know how to apply in my situation:
我已经尝试过此代码,但不知道如何在我的情况下应用:
NorthwindDataSet.CustomersRow newCustomersRow =
northwindDataSet1.Customers.NewCustomersRow();
newCustomersRow.CustomerID = "ALFKI";
newCustomersRow.CompanyName = "Alfreds Futterkiste";
northwindDataSet1.Customers.Rows.Add(newCustomersRow);
The problem is that shows an error saying it does not recognize the dataset... The erros is : "The name " Ds_Admissibilidade" does not exist in the current context 问题是显示错误,表明它无法识别数据集。错误是:“名称” Ds_Admissibilidade“在当前上下文中不存在
A DataSet
is a disconnected copy of the data. 一个
DataSet
是数据的一个副本断开。 It forgets if the data originated from database, an xml file or anything else. 它会忘记数据是否源自数据库,xml文件或其他任何内容。 When you add rows to the
DataSet
, you only change the in-memory copy, not the original source. 将行添加到
DataSet
,仅更改内存中的副本,而不更改原始源。
You need some mechanism to update the source. 您需要某种机制来更新源。 For databases, a table adapter or dataadapter will do this for you.
对于数据库,表适配器或数据适配器将为您完成此操作。 For a file source, you need to serialize the
DataSet
to the file, much the reverse of the way you read in in first place. 对于文件源,您需要将
DataSet
序列DataSet
到文件,这与您一开始就读入的方式相反。
Hope this helps :) 希望这可以帮助 :)
DataRow newRow = titulos.Tables["tb"].NewRow();
newRow["titulo1"] = "titulo1";
newRow["titulo2"] = "titulo2";
newRow["titulo3"] = "titulo3";
titulos.Tables["tb"].Rows.Add(newRow);
Make sure you're setting all the values of the non nullable parameters. 确保设置了所有不可为空的参数的值。 If you're using another instance of the dataset "titulos" use ImportRow instead of Add function.
如果您正在使用数据集“ titulos”的另一个实例,请使用ImportRow而不是Add函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.