使用C#和一个DataSet更新远程数据库。

huangapple go评论75阅读模式
英文:

Update a remote database using C# and a DataSet

问题

我有一个远程的Oracle数据库,我对其中的某个表感兴趣。对于我的C#程序,我使用VS DataSet设计器创建了一个DataSet,并为该表添加了一个TableAdapter

现在,我需要向该数据库表中添加一行新数据。起初,我尝试在我的数据集适配器中添加一个插入行的查询,如下所示:

使用C#和一个DataSet更新远程数据库。

然后,为了访问它,我使用了这段代码:

DataSetTableAdapters.INVENTURTableAdapter inventur_adapter = new INVENTURTableAdapter();

inventur_adapter.InsertQuery(arg1, arg2, arg3, ...);

这没有引发任何错误,但也没有更新远程数据库。

之后,我尝试了这里列出的一种方法:

DataSet ds = new DataSet();
DataSet.INVENTURRow newInventurRow = ds.INVENTUR.NewINVENTURRow();

newInventurRow.SOMEARG = value;
...
ds.NIESSING.Rows.Add(newInventurRow);

这也没有引发任何错误,但同样没有更新远程数据库。

我猜在这两个示例中,问题在于我需要创建数据集的新实例,但我不明白如何将新创建的数据集实例与远程表"merge"(合并)起来。

这里他们使用以下行来保存更改:

this.regionTableAdapter.Update(this.northwindDataSet.Region);

所以根据我的示例,应该是:

DataSetTableAdapters.INVENTURTableAdapter.Update(DataSet.INVENTUR);

问题是我不能使用"DataSet.INVENTUR"。我只能使用INVENTURDataTable,但这不起作用。

或者我可以这样做:

DataSetTableAdapters.INVENTURTableAdapter.Update(ds.INVENTUR);

这将引发错误:

需要非静态对象引用

我如何实际保存更新后的数据呢?

英文:

I have a remote Oracle database and a certain table that I am interested in. For my C# program, I created a DataSet using VS DataSet designer, and added a TableAdapter for that table.

Now I need to add a new row to that database table. At first, I tried to add insert a row query in my dataset adapter like this:

使用C#和一个DataSet更新远程数据库。

And then, in order to access it, I used this code:

DataSetTableAdapters.INVENTURTableAdapter inventur_adapter = new INVENTURTableAdapter();

inventur_adapter.InsertQuery( arg1, arg2, arg3, ...);

which didn't throw any errors, but it didn't update the remote database either.

After this I tried a method listed here:

 DataSet ds = new DataSet();
 DataSet.INVENTURRow newInventurRow = ds.INVENTUR.NewINVENTURRow();

 newInventurRow.SOMEARG = value;
 ...
 ds.NIESSING.Rows.Add(newInventurRow);

which also didn't throw any errors, and also did not update the remote database.

I guess in both examples it is because I need to create a new instance of that dataset, yet I cannot understand how can "merge" newly created instance of dataset with that remote table

Here They use this line to save changes:

this.regionTableAdapter.Update(this.northwindDataSet.Region);

so with my example it should be:

DataSetTableAdapters.INVENTURTableAdapter.Update(DataSet.INVENTUR);

The problem is that I can't use "DataSet.INVENTUR". I can only use INVENTURDataTable, which does not work

or I can do it this way:

DataSetTableAdapters.INVENTURTableAdapter.Update(ds.INVENTUR);

Which will throw an error:

> An object reference is required for non-static

How can I actually save the updated data?

答案1

得分: 1

以下是我用来解决这个问题的方法:

 DataSet dataSet = new DataSet();
 DataSetTableAdapters.INVENTURTableAdapter adapter = new DataSetTableAdapters.INVENTURTableAdapter();
 DataSet.INVENTURGRow newInventurRow = dataSet.INVENTUR.NewINVENTURGRow();
            
            newInventurRow.<COLUMNNAME1> = <VALUE1>
            ...
            newInventurRow.<COLUMNNAME99> = <VALUE99>

adapter.Update(dataSet.INVENTUR);

也许不是最佳的方法,但这是我唯一设法解决问题并使其正常工作的方式。

英文:

Here is the way how I managed to solve this:

 DataSet dataSet = new DataSet();
 DataSetTableAdapters.INVENTURTableAdapter adapter = new DataSetTableAdapters.INVENTURTableAdapter();
 DataSet.INVENTURGRow newInventurRow = dataSet.INVENTUR.NewINVENTURGRow();
            
            newInventurRow.<COLUMNNAME1> = <VALUE1>
            ...
            newInventurRow.<COLUMNNAME99> = <VALUE99>

adapter.Update(dataSet.INVENTUR);

Maybe not the optimal way to do this, but it is the only way I managed to solve it and it works.

huangapple
  • 本文由 发表于 2023年3月1日 16:09:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定