简体   繁体   English

在现有数据集上插入行

[英]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.

相关问题 LINQ:将新数据集插入与现有实体的多对多关系中 - LINQ: Insert new dataset into many-to-many relation with existing entities 如何将一列插入到两个现有列之间的数据集中? - How does one insert a column into a dataset between two existing columns? 批量插入现有表-仅插入新行 - Bulk insert into existing table - only insert new rows 如何使用存储过程更新现有行并插入新行? - How to update existing rows and insert new rows using stored procedure? 插入数据集 - insert into dataset SqlDataAdapter.Update(dataset)方法插入新行,但不更新现有行 - SqlDataAdapter.Update(dataset) method inserts new rows but does not updates existing rows 如何在不使用命令生成器的情况下将 DATASET 中的行插入到我的 DATABASE 中? - How do I insert Rows from my DATASET to my DATABASE without using command builder? 如何将多行数据从数组/列表插入SQL Server(DataSet,DataTable) - How to insert many rows of data from arrays/lists to SQL Server (DataSet, DataTable) 使用DataAdapter .Update插入/更新数据集中的行(不基于数据库PK)问题 - Using DataAdapter .Update to insert/upd rows in dataset (not based on DBs PK) problem 将数据集插入表PostgreSQL - Insert dataset to a table PostgreSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM