简体   繁体   English

使用Web表单将数据插入多个表

[英]inserting data into multiple tables using a web form

i would like to know what is the standard/best way of doing the following: 我想知道执行以下操作的标准/最佳方式是什么:

i have a form web app in asp.net and using C# 我在asp.net中有一个表单Web应用程序并使用C#

the user will enter data into the form and click INSERT and it will insert data into 4 different tables. 用户将数据输入表单并单击INSERT,它将数据插入4个不同的表中。

the fields are: 这些领域是:

primarykey, animal, street, country

the form allows for multiple animals, multiple streets and multiple countries per primarykey. 该表格允许多个动物,多个街道和每个主键的多个国家/地区。 so when i have data like this: 所以,当我有这样的数据:

[1],[rhino,cat,dog],[luigi st, paul st], [russia,israel]

i need it inserted into tables like this: 我需要将它插入到这样的表中:

table1:
1,rhino
1,cat
1,dog

table2:
1,luigi st
1, paul st

table3:
1,russia
1,israel

questions 问题

  1. I'm at a total loss on how to do this. 我完全不知道如何做到这一点。 if i just had one table and one set of data per primary key i would just use the InsertQuery and do it this way, but since it is multiple tables i don't know how to do this?? 如果我只有一个表和每个主键一组数据我只会使用InsertQuery并以这种方式做,但因为它是多个表我不知道如何做到这一点?

  2. what control(s) should i use in order to allow user to input multiple values? 我应该使用哪些控件来允许用户输入多个值? currently i am just using textboxes and thinking of separating the entries by semi colons, but that's probably not the right way. 目前我只是使用文本框并考虑用半冒号分隔条目,但这可能不是正确的方法。

I wanted to recommend that you take advantage of the new multirow insert statement in SQL 2008 so that you can just pass a sql statement like this: 我想建议你利用SQL 2008中新的multirow insert语句,这样你就可以像这样传递一个sql语句:

INSERT INTO table1(id,animal_name) values (1,cat),(1,dog),(1,horse)... 

To your SqlCommand but I don't know how to build a statement like that w/o risking being victim of a SQL Injection Attack. 对于你的SqlCommand但我不知道如何构建一个这样的语句,而不会冒成SQL注入攻击的受害者。

Another alternative is to define data table types in your sql database: 另一种方法是在sql数据库中定义数据表类型: 在此输入图像描述

在此输入图像描述

And then construct a DataTable in C# that matches your datatable type definition: 然后在C#中构造一个与您的数据表类型定义匹配的DataTable:

DataTable t = new DataTable();
t.Columns.Add("id");
t.Columns.Add("animal_name");
foreach(var element in your animals_list)
{
   DaraRow r = t.NewRow();
   r.ItemArray = new object[] { element.id, element.animal_name };
   t.Rows.Add(r);
}

// Assumes connection is an open SqlConnection.
using (connection)
{
    // Define the INSERT-SELECT statement.
    string sqlInsert = "INSERT INTO dbo.table1 (id, animal_name) SELECT nc.id, nc.animal_name FROM @animals AS nc;"

    // Configure the command and parameter.
    SqlCommand insertCommand = new SqlCommand(sqlInsert, connection);
    SqlParameter tvpParam = insertCommand.Parameters.AddWithValue("@animals", t);
    tvpParam.SqlDbType = SqlDbType.Structured;
    tvpParam.TypeName = "dbo.AnimalTable";

    // Execute the command.
    insertCommand.ExecuteNonQuery();
}

Read more here . 在这里阅读更多

Or if you are familiar with Stored Procedures, same as previous suggestion but having the stored procedure receive the DataTable t as parameter. 或者,如果您熟悉存储过程,与先前的建议相同,但使存储过程接收DataTable作为参数。

If none of the above work for you, create a SqlTranscation from the Connection object and iterate through each row of each data set inserting the record in the appropriate table and finally commit the transaction. 如果以上都不适合您,请从Connection对象创建一个SqlTranscation,并遍历每个数据集的每一行,在相应的表中插入记录,最后提交事务。 Example here. 这里的例子。

Use Checkboxes on the front end. 使用前端的复选框。 Have a service/repository to save the user data. 有一个服务/存储库来保存用户数据。 Something like the following: 类似于以下内容:

public void UpdateUserAnimals(Guid userId, string[] animals)
{
    using (SqlConnection conn = new SqlConnection("connectionstring..."))
    {
        using (SqlCommand cmd = new SqlCommand("Insert Into UserAnimals(UserId, Animals) values (@UserId, @Animal)"))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@UserId", userId);
            foreach(string animal in animals)
            {
                cmd.Parameters.AddWithValue("@Animal", animal);
                cmd.ExecuteNonQuery();
            }
        }
    }
}

There are more complex solutions, but this is a simple one. 有更复杂的解决方案,但这很简单。

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

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