且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL数据库的单个字段中存储不同的值

更新时间:2022-12-11 19:53:17

使用varchar字段并为您的数据提供格式,例如"radiovalue | dropdown1value | dropdown2value"并存储像往常一样在现场.当您阅读该字段时,请输入一个字符串.在``|''上分割以分隔值.您也可以将数据存储为xml或JSON格式,具体取决于您.

但是总的来说,这是一个坏主意,因为您可能想查询数据,例如,所有以"1"作为第一个下拉值的记录,您将无法查询.***将数据存储在不同的字段中,或者在主记录和与其相关的所有字段之间使用一对多的关系.
Use a varchar field and come up with a format for your data such as "radiovalue|dropdown1value|dropdown2value" and store that in the field as normal. When you read the field do a string.Split on the ''|'' to separate the values. You could also store the data as xml, or maybe in JSON format, it is up to you.

However in general this is a bad idea as down the line you might want to query the data, eg all records with "1" as the first dropdown value and you won''t be able to. It is much better to store data in different fields, or use a one to many relationship between the master record and all the fields that are relevant to it.


Don''t.
这样做是一件令人讨厌的事情,因为它会在以后引起重大问题-每次要使用它的任何部分时,都必须解压"单个列.
用三列替换单列,并分别存储每个值.似乎还需要更多工作,但以后可以节省大量时间和精力.
Don''t.
It''s a nasty thing to do because it causes major problems later - you have to "unpack" the single column each time you want to work with any part of it.
Replace the single column with three columns, and store each value separately. It may seem like a little more work, but it saves a heck of a lot of time and effort later.


有两种方法可以解决问题.

1.将所有(Radiabutton,DropDownBox)值合并为一个,然后将该值传递给SQL查询.

There are two methods in which you can solve your problem.

1. Combine all (Radiabutton ,DropDownBox) value into one and pass that value to SQL query.

string dropDownBoxValue1 = string.Empty;
string dropDownBoxValue2 = string.Empty;
string radioButton1Value = string.Empty;

string combineAllValue = string.Empty;


bool isChecked = radioButton1.Checked;


if(isChecked )
  radioButton1Value=radioButton1.Text;
  
  .......
  .......
  
dropDownBoxValue1 = Convert.ToString(DropDownBox1.SelectedValue);

dropDownBoxValue2 = Convert.ToString(DropDownBox2.SelectedValue);

combineAllValue=radioButton1Value +","+dropDownBoxValue1+","+dropDownBoxValue2 



注意:--- CombineAllValue将此值传递给查询String.


2.合并存储过程中的所有值,然后存储到表中.



Note :- combineAllValue pass this value to query String.


2. Combine all values in Stored Procedure and then store to table.