简体   繁体   English

使用文本框控件asp.net插入数据库

[英]Insert into database using textbox control asp.net

I'm trying to insert data from control textbox into a database table; 我正在尝试将控件文本框中的数据插入数据库表; however say the user selects that they want to textboxes; 但是说用户选择他们想要的文本框; then one ID in the textbox is entered in one row and the other ID is entered in another row in the database and I want it to entered as ID1 and ID2. 然后在数据库的一行中输入一个ID,在数据库的另一行中输入另一个ID,我希望将其作为ID1和ID2输入。 I have 3 columns that user can enter in the IDs; 我有3列用户可以输入ID; so for instance they select 1 textbox then the data in that one textbox is entered and other columns remain null. 因此,例如,他们选择1个文本框,然后输入该文本框中的数据,而其他列保持为空。

Update 更新资料

using (var command = new SqlCommand("PP_CreateIDNumber", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@usernum", lblSheet.Text);

                        int counter = 1;
                        foreach (TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>())
                        {
                                string seal = string.Format("@seal{0}", counter++);
                                command.Parameters.AddWithValue(seal, textBox.Text);

                                command.ExecuteNonQuery();
                         }

} }

foreach (TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>())
{
    using (SqlCommand comm = new SqlCommand("PP_CreateIDNumber", connection))
    {                  
            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@userNum", lblUser.Text);
            comm.Parameters.AddWithValue("@ID1", textBox.Text);
            comm.Parameters.AddWithValue("@ID2", textBox.Text);
            comm.Parameters.AddWithValue("@ID3", textBox.Text);

            comm.ExecuteNonQuery();
            comm.Parameters.Clear();
        }
    }    
}

Stored Procedure 储存程序

@usernum varchar(20),
    @ID1 nchar(10),
    @ID2 nchar(10),
    @ID3 nchar(10),



AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    insert into Student values(@usernum, @ID1,@ID2, @ID3)

在此处输入图片说明

If I understand correctly, then I believe you want something like this: 如果我理解正确,那么我相信你想要这样的东西:

    using (var command = new SqlCommand("PP_CreateIDNumber", connection)) {
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@userNum", lblUser.Text);

        int counter = 1;
        foreach(TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>()) {
            command.Parameters.AddWithValue($"@ID{counter++}", textBox.Text);
        }

        command.ExecuteNonQuery();
    }

If you do not have access to a framework version supporting string interpolation in the manner I have used it above, you can use string.Format("@ID{0}", counter++) and that should be sufficient for any framework version. 如果您无法以我上面使用的方式访问支持字符串插值的框架版本,则可以使用string.Format("@ID{0}", counter++) ,这对于任何框架版本都足够了。

You should additionally consider the order in which you should iterate over your textboxes and whether you have a need to impose a specific order. 您还应该考虑在文本框中进行迭代的顺序以及是否需要强加特定顺序。 You should also consider validating that the number of textboxes does not exceed the number of foreign key columns to which you will be inserting data. 您还应该考虑验证文本框的数量不超过您要向其插入数据的外键列的数量。 Lastly, you should consider whether the design of this table is actually optimal. 最后,您应该考虑该表的设计是否实际上是最佳的。 Since you are holding many foreign keys here, you should consider having a table designed for a one-to-many relationship. 由于您在这里拥有许多外键,因此您应该考虑设计一个用于一对多关系的表。

Not having intimate knowledge of your schema makes it difficult to provide a specific suggestion here, but consider a table that has a UserNum column and simply an Id column (potentially even an additional column that indicates the sequence/order of the identities). 由于不了解您的架构,因此很难在此处提供具体的建议,但请考虑一个表,该表具有一个UserNum列和一个Id列(可能甚至还有一个指示身份顺序/顺序的列)。 Instead of using one row to hold the data, use many rows. 与其使用一行来保存数据,不如使用多行。 I do not know your requirements and again your schema, so I feel sheepish about pushing you to a potentially unnecessary redesign of a table, but you should at least consider whether it is applicable here. 我不知道您的要求,也不知道您的模式,因此我对于将您推向可能不必要的表重新设计感到不满意,但是您至少应该考虑它是否适用于此。

Lastly, consider not performing data access within your user interface. 最后,考虑不要在用户界面内执行数据访问。 Have your user interface reference a separate assembly that holds classes that understand how to provide your UI with the data it requires. 让您的用户界面引用一个单独的程序集,该程序集包含一些类,这些类理解如何为UI提供所需的数据。

All of that being said, I believe the code example gets you where you want to go at the moment. 综上所述,我相信代码示例可以带您到达现在想要去的地方。

Edit after substantial questions and conversation: 经过实质性问题和对话后进行编辑:

You need to supply the additional parameters for the number of textboxes that have not been chosen. 您需要为尚未选择的文本框数量提供其他参数。

int maxPossibleTextBoxCount = 3;
int selectedTextBoxCount = ContentPlaceHolder1.Controls.OfType<TextBox>().Count();
int emptyTextBoxCount = maxPossibleTextBoxCount - selectedTextBoxCount;

using (var command = new SqlCommand("PP_CreateIDNumber", connection)) {
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@userNum", lblUser.Text);

    int counter = 1;

    // Here we add the parameters for the selected textboxes.
    foreach(TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>()) {
        command.Parameters.AddWithValue($"@ID{counter++}", textBox.Text);
    }

    // Here we add the parameters for the non-selected textboxes.
    if(emptyTextBoxCount > 0) {
        for(int i = 0; i < emptyTextBoxCount - 1; i++) {
            command.Parameters.AddWithValue($"@ID{counter++}", System.DBNull.Value);
        }
    }

    command.ExecuteNonQuery();
}

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

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