简体   繁体   中英

How to Add Data Dynamically to sql server table from dynamically generated fields on Webform?

I have a webform (ASPX) with some fields which collects user information and stores the information in the table in the DB(sql server 2008). Now the problem arises when user wants to add extra data.

Suppose the user was entering details of his property (5 fields on the webform from which data is captured and goes to 5 fields in table in DB),

Now if user has more than one property and he wants to enter the details of that too (He clicks on Add button which adds extra 5 fields for the another property details but the table has still 5 fields which stores data for first property details) So from what i understand these are options:

1) Keep the extra fields in table while table creation. Like 15 fields for entering details of 3 types of property (But this has two drawbacks 1. Too big table 2. What if the fields are still not sufficient)

2) Alter my table according to the number of fields added by user after submitting the form.

3) Create fields dynamically in the table to add the data at the runtime (this seems like a feasible option but i have no idea how to achieve that)

Any other options or suggestions with example for the above cases will be helpful

You have several options

Use a schema less database for this information after all your data is schema less so a relational database isn't a good fit. Can be tricky if you don't control your tech stack.

Fake schema less, serialize the object to XML or JSON and drop it in a field. Could be a problem if you want to query it though.

Create a data structure where you have one table for the object and another for its properties, so one object has many properties. The properties table has one entry for each property that instance has.

eg

Thing Table

ThingID int
Name varchar

ThingProperties Table

ThingPropertID int
ThingID int
Name varchar
Value varchar

You can query this and its relatively easy to build a page from and save the data back.

If you know they are always in batches of five you could have five columns in your properties table and each row is a batch but that may make like more complicated.

Fundamentally each piece of information has many properties and you model needs to capture that relationship. Trying to keep altering tables on the fly is going to be a very bad idea for all sorts of reasons.

第三个示例用于关系数据库,创建两个表结构,其中明细表为父级每行保留一个属性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM