简体   繁体   English

如何从Webform上动态生成的字段向SQL服务器表动态添加数据?

[英]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). 我有一个带有某些字段的Webform(ASPX),用于收集用户信息并将信息存储在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), 假设用户正在输入其属性的详细信息(网络表单上的5个字段用于捕获数据,并转到DB表中的5个字段),

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: 现在,如果用户具有多个属性,并且他也想输入该属性的详细信息(他单击“添加”按钮,该按钮将为其他属性详细信息添加额外的5个字段,但是表格中仍有5个字段存储了第一个属性详细信息的数据)据我了解,这些是选项:

1) Keep the extra fields in table while table creation. 1)在创建表时将多余的字段保留在表中。 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) 像15个字段一样,用于输入3种类型的属性的详细信息(但这有两个缺点:1.表太大。2.如果这些字段仍然不够用怎么办)

2) Alter my table according to the number of fields added by user after submitting the form. 2)提交表格后,根据用户添加的字段数更改我的表格。

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) 3)在表中动态创建字段以在运行时添加数据(这似乎是一个可行的选择,但我不知道如何实现)

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. 减少伪造的架构,将对象序列化为XML或JSON并将其放在字段中。 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 ThingProperties表

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. 由于种种原因,试图保持动态更改表将是一个非常糟糕的主意。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法从动态生成的字段(字段名称、字段类型)创建 SQL 表 - Is there a way to create an SQL Table from a dynamically generated fields (Field name, Field type) 动态将数据写入SQL Server表中的列 - Write data to columns in SQL Server table dynamically 在Webform中显示SQL Server表数据 - Displaying SQL Server table data in webform 如何使用c#从文件系统和SQL Server中删除动态生成的数据库文件(.MDF和.LDF) - How to delete a dynamically generated database file (.MDF and .LDF) from the filesystem and from SQL server using c# 如何使用JavaScript在ASP.NET Web表单上动态添加文本框 - How to add textboxes dynamically on a asp.net webform using javascript SQL Server如何与Web表单交换数据 - How does SQL Server exchange data with a webform 如何在动态添加的表中添加动态链接 - How to add a dynamically link in a dynamically added table 如何从表中捕获数据(动态捕获到数据库) - How to catch data from table (dynamically to database) Combobox动态添加行并从SQL Server存储过程下拉 - Combobox add rows dynamically and dropdown from SQL Server stored procedure 设置一个 <select> SQL Server数据库表中的动态下拉列表 - Setup a <select> dropdown list dynamically from a SQL Server database table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM