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

问题描述


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


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


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


这将是关系图



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


 


所以每个联系人都有两个不同的电子邮件和电话号码,由其外国人与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
// ...
}

解决方案

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
	// ...
}

解决方案


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

08-30 21:09