如何在SQL数据库的单个字段中存储不同的值 [英] How store different values at single field in SQL database

查看:215
本文介绍了如何在SQL数据库的单个字段中存储不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Radiabutton,两个DropDownBox,我想将所有值存储在sql中的Single Field中.请帮助..,

我尝试过的事情:

我不知道,请帮助我

i am using one Radiabutton ,Two DropDownBox,I want to store All values in Single Field in sql .please help..,

What I have tried:

i have no idea,please please help me

推荐答案

使用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.


这篇关于如何在SQL数据库的单个字段中存储不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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