最有效地填写相关表格 [英] Fill related tables most efficiently

查看:75
本文介绍了最有效地填写相关表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我认为这个问题应该已经解决了数千次,并在很多论坛中都有记录。但像往常一样,通过搜索关键字来找到合适的解决方案是耗费时间和令人沮丧的。


所以请耐心等待,当我再次发布一次又一次时。


考虑带有联系人的表与带有电子邮件和语音的其他两个表之间的经典一对多关系。当然,联系人可以有几个电子邮件地址以及几个电子邮件。


这将是关系图



我们假设数据库中的表格中填充了此样本数据


 


所以每个联系人都有两个不同的电子邮件和电话号码,由其外国人与ContactTable相关联。密钥ContactID。


 


当我想修改这些数据时,我可以使用带有DataTable实例的DataSet将其加载到内存中System.Data。


所以让我们从这种代码开始


 
private void LoadData()
{
DataTable ContactTable = new DataTable();
ContactTable.Columns.Add(" ID" );
ContactTable.Columns.Add(" FirstName" );
ContactTable.Columns.Add(" LastName" );
ContactTable.Columns.Add(" Gender" );

DataTable EmailTable = new DataTable();
ContactTable.Columns.Add(" ID" );
ContactTable.Columns.Add(" ContactID" );
ContactTable.Columns.Add(" Address" );

DataTable PhoneTable = new DataTable();
ContactTable.Columns.Add(" ID" );
ContactTable.Columns.Add(" ContactID" );
ContactTable.Columns.Add(" Number" );

DataSet ContactSet = new DataSet();
ContactSet.Tables.Add(ContactTable);
ContactSet.Tables.Add(EmailTable);
ContactSet.Tables.Add(PhoneTable);

//现在用Gender == Mail
// ...
}

解决方案


根据您的示例代码,您使用的是无类型数据集而不是类型化数据集,对吗?  对于unytped数据集,我们可能需要在数据集中手动创建主键和外键关系。  另外,要加载相关的
数据行,我们还需要编写相应的查询,例如


" SELECT E.ID,E.ContactID,E.Address FROM EmailTable as E JOIN ContactTable为C ON E.ContactID = C.ID ...."


如果我们使用类型化数据集,则可以更容易。 在MSDN文档中有更多的好参考,
http:// msdn。 microsoft.com/en-us/library/ms171913.aspx


美好的一天!


谢谢


Hi,

I thought the problem should be already solved a thousand times and documented in many forums. But as usual, to find the right solution by searching with keywords is timeconsuming and frustrating.

So please be patient, when I post it a thousand and one time again.

Think about the classical one-to-many relationship between a table with contacts, and two other tables with email and phonenumbers. Of course a contact can have serveral emailaddresses as well as several phonenumbers.

This would be the relationsship diagram

And let's assume the tables in database are filled with this sample data

 

So every contact has two different emails and phonenumbers, associated to the ContactTable by its foreign  key ContactID.

 

When I want to modify this data, I can load it into memory using a DataSet with DataTable instances from namespace System.Data.

So let's start with this kind of code

private void LoadData()
{
	DataTable ContactTable = new DataTable();
	ContactTable.Columns.Add("ID");
	ContactTable.Columns.Add("FirstName");
	ContactTable.Columns.Add("LastName");
	ContactTable.Columns.Add("Gender");
	
	DataTable EmailTable = new DataTable();
	ContactTable.Columns.Add("ID");
	ContactTable.Columns.Add("ContactID");
	ContactTable.Columns.Add("Address");

	DataTable PhoneTable = new DataTable();
	ContactTable.Columns.Add("ID");
	ContactTable.Columns.Add("ContactID");
	ContactTable.Columns.Add("Number");
	
	DataSet ContactSet = new DataSet();
	ContactSet.Tables.Add(ContactTable);
	ContactSet.Tables.Add(EmailTable);
	ContactSet.Tables.Add(PhoneTable);

	// now fill tables for all contacts with Gender == Mail
	// ...
}

解决方案

Hi,

Based on your sample codes, you are using untyped dataset instead of typed dataset, right?   For unytped dataset, we may need to manually create the primary key and foreign key relationships in the dataset.   Also, to load the related datarows we also need to write corresponding queries, e.g.

"SELECT E.ID, E.ContactID, E.Address FROM EmailTable as E JOIN ContactTable as C ON E.ContactID = C.ID ...."

If we use typed dataset, it can be easier.  There is more good references in MSDN documents, http://msdn.microsoft.com/en-us/library/ms171913.aspx.

Good day!

Thanks


这篇关于最有效地填写相关表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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