ADO.NET:如何正确绑定控件上两个表之间的1:n关系 [英] ADO.NET: How do I bind correctly an 1:n relation between two tables on a control

查看:61
本文介绍了ADO.NET:如何正确绑定控件上两个表之间的1:n关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基于一个小项目,我正在自己研究ADO.NET和SQLite.
我使用过的数据库系统:通过 SQLite提供程序
的SQLite
更多信息
我使用#develop,因此对此没有设计时支持.它必须全部用代码完成.

这是Windows窗体应用程序.

我已经链接了一个表格游戏"和一个表格流派".
每个游戏记录都可以分配一个类型".

我想输出表"games"的数据,当然,要输出表"genre"的"ID"字段,而不是表"genre"的"ID",即"Genre"."Name".

问题
我所有的尝试来实现它,都无法正常工作,而google或msdn无法帮助.


我进行如下操作

所以我创建了DataAdapter

Hi,

based on a small project I''m working myself into ADO.NET and SQLite.
My-used database system: SQLite via the SQLite provider

edit: further information
I use #develop and so i have no design-time support for this. It has to be done all in code.

It''s an windows forms application.

I have linked a table "Games" and a table "genre".
Every game record can have a "genre" assigned.

I would like to output the data of table "games" and of course, instead of the "ID" of the table "genre" its "name" field, that is "Genre"."Name".

Problem
All my tries to implement it, didn''t work and google or msdn couldn''t help.


I proceed as follows

So I create the DataAdapter

private void create adapter ()
{
rating SQLiteDataAdapter adapter = new ();

/ / A table mapping names the DataTable.
RatingsAdapter.TableMappings.Add ("Table", rating table);
RatingsAdapter.TableMappings.Add ("Table1", table games);
RatingsAdapter.TableMappings.Add ("Table2", test table);
RatingsAdapter.TableMappings.Add ("Table3" genre table);

/ / Create the SelectCommand.
DbCommand SelectCommand = GRData.DataProvider.CreateCommand ();
SelectCommand.Connection = GRData.DataConnection;
SelectCommand.CommandText =
"SELECT * FROM" + table + rating "," +
"SELECT * FROM" + table + games "," +
"SELECT * FROM" + table + tester "," +
"SELECT * FROM" + table + genre ",";
RatingsAdapter.SelectCommand = SelectCommand;

/ / Creates all other commands
rating GRData.DataProvider.CreateCommandBuilder commands = ();
RatingsCommands.DataAdapter rating = adapter;
}



在这里填充数据集并设置表之间的关系



Here I fill the dataset and set the relations between the tables

private void select ratings ()
{
/ / Fill Dataset
RatingsAdapter.Fill (RatingsDataset);

DataRelation relation;

...

/ / Specifiy relation between games and Genres
DataColumn col_GenresID = RatingsDataset.Tables [genre table] Columns ["id"];.
DataColumn col_GamesGenreID = RatingsDataset.Tables [games table] Columns ["genre"],.
Relation = new DataRelation ("rel_GamesGenre" col_GenresID, col_GamesGenreID);
RatingsDataset.Relations.Add (relation);

...

}



这是对各个控件的绑定

说明:GenreText是Windows.Forms.Label.



Here is the binding to individual controls

Description: GenreText is a Windows.Forms.Label.

private void BindControls ()
{
...
GenreText.DataBindings.Add ("Text", RatingsDataset, String.Concat (games table, ". Rel_GamesGenre ";));
...
}



错误消息
System.ArgumentException:无法绑定到数据源上的属性或列rel_GamesGenre.


我做错了什么?

谢谢和问候
Antonio



Error Message
System.ArgumentException: Can not bind to the property or column rel_GamesGenre on the data source.


What am I doing wrong?

Thanks and Greetings
Antonio

推荐答案

以下是一些 Master/详细教程 [ ^ ]

另一个恕我直言更好的解决方案是忘掉DataSet.创建自己的业务对象,该业务对象可直接与提供程序的低级组件(DbDataReader,DbCommand等的后代)一起使用.向您公开详细信息集合,为System.ComponentModel.BindingList< T>.

使用BindingSource为您的表单应用程序设置绑定.

这种方法在Forms和asp.net上都可以很好地发挥作用.


希望对您有所帮助.



问候
Espen Harlinn
Here is a few Master/Detail tutorials[^]

Another, and IMHO better solution, is to forget about DataSet. Create your own business objects that works directly with the low level components of the provider (descendants of DbDataReader, DbCommand, and so on). Expose you details collections as System.ComponentModel.BindingList<T>.

Use BindingSource to set up bindings for your forms application.

This approach tends to work well with both Forms and asp.net


I hope this will prove helpfull.



Regards
Espen Harlinn


我知道有很多可能的解决方案来建立一种工作机制,以获取有效的数据库和Windows窗体以及我们在它们之间发现的所有问题,例如关系,绑定和等等.

我只是选择了一种可能性,以了解更多有关如何在.Net和数据库中使用C#的知识.由于我的项目很小,因此我需要一个非常快速且容易的项目,这就是为什么我认为嘿,请尝试将标准ADO.NET与简单的数据库提供程序一起使用.

而且我很快就做好了,因为那里有许多教程和示例.但是,当我谈到关系时,开发人员似乎唯一要做的就是将它们与dataGridView或dataView绑定.大多数使用Visual Studio的设计时GUI使其变得相当简单.

我正在开发一个没有Grid的简单应用程序,但是有一个TreeView并带有详细视图,一个显示,一个编辑.因此,此详细信息视图填充有用于显示数据的标签和用于编辑数据的文本框.

因此,将数据集中的表中的简单列绑定到文本框很容易并且可以正常工作.但是,如果该列与另一个表有关系,那么您将只能在其中找到ID,该如何获取相关表的名称或其他字段呢?

我已经完成了关系,为什么它没有显示我喜欢的领域?

这是经过很多天和紧张之后的解决方案……它是如此简单,以至于我不明白在其他任何其他教程中都找不到它.


I know that there are a lot of possible solutions to set up a working mechanism to get working databases and windows forms and all the problems we find between it, like relations, bindings and so on.

I have chosen just one possiblity to learn a bit more how i can go on with c# in .Net and databases. As my project is a very small one i needed a very fast and easy possiblity, that is why i thought hey, try using standard ADO.NET with an simple database provider.

And i went fast and good with it, as there are many tutorials and samples out there. But when I came to the point of relations the only thing developer seem to do is to bind them with an dataGridView or dataView. The most use Visual Studio that makes it rather simple with its design-time GUI.

I''m working on an simple app without an Grid, but an TreeView and with detailed views, one to show, one to edit. So this detail views are populated with labels to show the data and textboxes to edit the data.

So, binding a simple column from a table in dataset to an textbox is easy and works fine. But what if the column is a relation to an other table, so you will find only the id in it, how do I get the name or an other field of the related table?

I have done the relation why doesn''t it show up the field i like?

Here is the solution after a lot of days and nerves...and it is so simple that i can''t understand to have found it in any other tutorial else where.


// Bind Genres to Games
MasterSource = new BindingSource(RatingsDataset, GenresTable);
DetailsSource = new BindingSource(MasterSource, "rel_GamesGenre");



在这里,我在游戏"和流派数据"之间设置了绑定.
发生的情况是,当游戏记录发生变化时,类型将指向
到我之前设置的foreignKey关系的记录.
Thas就是全部.标签的流派字段绑定将如下所示:



Here I set an binding between the Games and Genres Data.
What happens is, when record of Games changes the Genres will point
to the record of the foreignKey relation that i set before.
Thas is all. The binding to a field of Genres for the label will look like this:

GenreText.DataBindings.Add("Text", RatingsDataset, GenresTable + ".name");



如果没有BindingSource,则流派将指向表中的第一条记录.

感谢Espen试图帮助我.

问候



Without the BindingSource the Genres would point to the first record in the table.

Thanks to Espen who tried to help me out.

Regards


这篇关于ADO.NET:如何正确绑定控件上两个表之间的1:n关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆