如何使用c#代码在sql中的一行中插入多个id [英] How to insert more than one id to a row in sql using c# code

查看:107
本文介绍了如何使用c#代码在sql中的一行中插入多个id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在向列中添加多个ID时遇到问题。我的项目是我需要添加人员的详细信息。在这个我用1个人可以添加多个国家名称。例如一个人名:abc可以将他的国家名称添加为印度和美国。国家/地区名称是通过下拉列表获取的。在我的项目中,所有人都在第一个选项卡中使用选项卡控件我可以添加名称,在下一个选项卡中有一个选择国家/地区名称的下拉列表,有一个添加按钮,所以我可以添加多个国家/地区名称到一个特别的名字。因此,有一个主提交按钮,用于所有选项卡,所有插入的数据都应插入数据库。示例:name:abc country:1,2。国家保存为id。

Hi,
I have a problem in adding more than one id to a column. My project is that I need to add details of person. In this I use 1 person can add more than one country name. For example a person name: abc can add his country names as India and USA. The country name is getting by drop down list. In my project all are using a tab control in first tab I can add name and in next tab there is a drop down list for country selection on selecting the country name there is a add button so I can add more than one country name to a particular name. So there is a main submit button which is for all the tabs all the data inserted should be inserted to database. example: name: abc country:1,2 . Country is saved as id.

推荐答案

不要。

除非你开始玩字符串(这会让生活变得更加复杂之后)每行每行只能存储一个值。

相反,创建另一个包含交叉引用的表:

原始表格:

Don't.
Unless you start playing with strings (and that makes life very much more complicated later) you can only store one value per column, per row.
Instead, create another table which contains the cross references:
Your original table:
UserID
UseName
...



新表:


New table:

UserId
Country

Users表中的每个条目的Countries表中都有几个条目。

我实际上走得更远,有三张桌子:

CountryNames:

You would then have several entries in the "Countries" table for each entry in the "Users" table.
I woudl actually go further, and have three tables:
CountryNames:

CountryID CountryName
   1      Wales
   2      France
   3      India
...

用户:

Users:

UserId    UserName
   1      Paul
   2      Pierre
   3      Sangit
...

UserCountries:

UserCountries:

ID        CountryID
   1      1
   1      2
   2      2
   3      1
   3      2
   3      3
...

你然后可以毫无问题地获得每个用户的国家列表。

You can then get a list of countries for each user without any problems.


这篇关于如何使用c#代码在sql中的一行中插入多个id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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