使用C#在SQL Server中插入整数值数组
问题描述:
我想在SQL数据库表中存储一个整数值数组,并将其存储为逗号分隔的值列表。(在一行中)。
有人可以建议一种方法来实现这个目的吗??
I want to store an array of integer values in a SQL database table,And store it as a comma separated list of values.(with in a single row).
Can some one suggest a method to accomplish this..?
答
如果我正确理解你的问题,如果它是逗号分隔的列表,你可以只转换字符串并将其存储在varchar字段中。
但是,我真的不建议这样做。在我看来,最好的解决方案是将每个值存储为数据库中的单独行。您可能需要一个新表,可能需要主表的外键,但以这种方式存储值将防止将来出现很多问题。例如,考虑您需要更改或删除列表中的值,这需要付出很多努力,并且可能容易出错。
If I understand your question correctly, if it's a comma separated list, you could just convert the list to string and store it in a varchar field.
However, I really wouldn't advice doing this. In my opinion the best solution would be to store each value as a separate row in the database. You may need a new table for that which may need a foreign key to the master table but storing the values this way will prevent a lot of problems in the future. For example consider of you need to change or delete values inside the list, this would require a lot of effort and would probably be error-prone.