C# 在数据库表中使用 DataTable 更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11561629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:19:39  来源:igfitidea点击:

Updating using DataTable in database table?

c#.netado.net

提问by Kishore Kumar

I have a table which has some 100-200 records. I have fetch those records into a dataset.

我有一张桌子,里面有大约 100-200 条记录。我已将这些记录提取到数据集中。

Now i am looping through all the records using foreach

现在我正在使用 foreach 遍历所有记录

dataset.Tables[0].AsEnumerable()

I want to update a column for each record in the loop. How can i do this. Using the same dataset.

我想为循环中的每条记录更新一列。我怎样才能做到这一点。使用相同的数据集。

采纳答案by Derek

I'm Assumng your using a Data Adapter to Fill the Data Set, with a Select Command?

我假设您使用数据适配器填充数据集,并使用选择命令?

To edit the data in your Data Table and save changes back to your database you will require an Update Command for you Data Adapter. Something like this :-

要编辑数据表中的数据并将更改保存回数据库,您将需要数据适配器的更新命令。像这样的东西:-

SQLConnection connector = new SQLConnection(@"Your connection string");

SQLAdaptor Adaptor = new SQLAdaptor();

Updatecmd = new sqlDbCommand("UPDATE YOURTABLE SET FIELD1= @FIELD1, FIELD2= @FIELD2   WHERE ID = @ID", connector);

You will also need to Add Parameters for the fields :-

您还需要为字段添加参数:-

Updatecmd.Parameters.Add("@FIELD1", SQLDbType.VarCHar, 8, "FIELD1");
Updatecmd.Parameters.Add("@FIELD2", SQLDbType.VarCHar, 8, "FIELD2");

var param = Updatecmd.Parameters.Add("@ID", SqlDbType.Interger, 6, "ID");
param.SourceVersion = DataRowVersion.Original;

Once you have created an Update Command with the correct SQL statement, and added the parameters, you need to assign this as the Insert Command for you Data Adapter :-

使用正确的 SQL 语句创建更新命令并添加参数后,您需要将其分配为数据适配器的插入命令:-

Adaptor.UpdateCommand = Updatecmd;

You will need to read up on doing this yourself, go through some examples, this is a rough guide.

你需要自己阅读,通过一些例子,这是一个粗略的指南。

The next step is to Enumerate through your data table, you dont need LINQ, you can do this :-

下一步是枚举您的数据表,您不需要 LINQ,您可以这样做:-

  foreach(DataRow row in Dataset.Tables[0].Rows)
    {
        row["YourColumn"] = YOURVALUE;
    }

One this is finished, you need to call the Update() method of yout Data Adapter like so :-

一个这样就完成了,您需要像这样调用您的数据适配器的 Update() 方法:-

DataAdapter.Update(dataset.Tables[0]);

What happens here, is the Data Adapter calls the Update command and saves the changes back to the database.

这里发生的是,数据适配器调用更新命令并将更改保存回数据库。

Please Note, If wish to ADD new rows to the Database, you will require am INSERT Command for the Data Adapter.

请注意,如果希望向数据库添加新行,您将需要数据适配器的插入命令。

This is very roughly coded out, from the top of my head, so the syntax may be slightly out. But will hopefully help.

这是非常粗略地编码出来的,从我的头顶开始,所以语法可能会略有出入。但希望会有所帮助。

回答by Steve Wellens

Like this:

像这样:

DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("ProductName");

table.Rows.Add(1, "Chai");
table.Rows.Add(2, "Queso Cabrales");
table.Rows.Add(3, "Tofu");

EnumerableRowCollection<DataRow> Rows = table.AsEnumerable();

foreach (DataRow Row in Rows)
    Row["ID"] = (int)Row["ID"] * 2;

回答by cshemby

The steps would be something like: - create a new DataColumn[^] - add it to the data table's Columns[^] collection - Create a DataRow [^] for example using NewRow[^] - Modify the values of the row as per needed using Item[^] indexer - add the row to Rows[^] collection - after succesful modifications AcceptChanges[^]

步骤类似于: - 创建一个新的 DataColumn[^] - 将其添加到数据表的 Columns[^] 集合 - 创建一个 DataRow [^] 例如使用 NewRow[^] - 按照每个修改行的值需要使用 Item[^] 索引器 - 将行添加到 Rows[^] 集合 - 成功修改后 AcceptChanges[^]

回答by cshemby

Add the column like below.

添加如下所示的列。

dataset.Tables[0].Columns.Add(new DataColumn ("columnname"));

dataset.Tables[0].Columns.Add(new DataColumn ("columnname"));

Update the columns values like below.

更新列值,如下所示。

for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { dataset.Tables[0].Rows[i]["columnname"] = "new value here"; }

for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { dataset.Tables[0].Rows[i]["columnname"] = "new value here"; }

Update Database

更新数据库

dataset.AcceptChanges();

dataset.AcceptChanges();

回答by Antonio Bakula

You should use original DataAdapter (adapterin code below) that was used to fill DataSet and call Update method, you will need CommandBuilder also, it depends what DB you are using, here is the example for SQL server :

您应该使用adapter用于填充 DataSet 和调用 Update 方法的原始 DataAdapter(在下面的代码中),您还需要 CommandBuilder,这取决于您使用的数据库,这是 SQL 服务器的示例:

SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(dataset);
dataset.AcceptChanges();

Here is the good example :

这是一个很好的例子:

http://support.microsoft.com/kb/307587

http://support.microsoft.com/kb/307587