简体   繁体   English

将多个记录插入到SQL Server

[英]Inserting more than one record to sql server

Guys i have a grid and need to retrieve text inputs from and then insert to the the database . 伙计们,我有一个网格,需要从中检索文本输入,然后将其插入数据库。 The grid looks like below 网格如下所示

From the above it permits user to pass as many rows to the database as he so desired. 从上面可以看出,它允许用户根据需要将尽可能多的行传递给数据库。 i use the method below. 我使用下面的方法。

   private void insert()
        {
            connection.Open();

            for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
            {
                string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";

                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@row1", gvAdditionalDetails.Rows[i].Cells[1].Text.Trim());
                cmd.Parameters.AddWithValue("@row2", gvAdditionalDetails.Rows[i].Cells[2].Text.Trim());
                cmd.Parameters.AddWithValue("@contactID", 39);
                cmd.ExecuteNonQuery();
            }
            connection.Close();
        }

The above method loop throw my grid , how ever is returning null for the columns type which has a dropdown selection and description which has a tetxbox control. 上面的方法循环引发了我的网格,如何为具有下拉选择的列类型和具有tetxbox控件的说明返回null。 I cant call this controls invidually because they are declared in a grid. 我不能单独调用此控件,因为它们是在网格中声明的。 How do i retrieve the text the selected item from the dropdown and the inserted text from the textbox. 如何从下拉菜单中检索所选项目的文本,并从文本框中检索插入的文本。 the code gvAdditionalDetails.Rows[i].Cells[1].Text.Trim() returns null. 代码gvAdditionalDetails.Rows[i].Cells[1].Text.Trim()返回null。

You need to refer to the control inside the gridview row. 您需要引用gridview行内的控件。

private void insert()
        {
            connection.Open();

            for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
            {
                string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";

                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@row1", ((DropdownList)gvAdditionalDetails.Rows[i].FindControl("DropDownListType")).SelectedItem.Text.Trim());
                cmd.Parameters.AddWithValue("@row2", ((TextBox)gvAdditionalDetails.Rows[i].FindControl("txtDescription")).Text.Trim());
                cmd.Parameters.AddWithValue("@contactID", 39);
                cmd.ExecuteNonQuery();
            }
            connection.Close();
        }
INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
INSERT INTO Table (Column1, Column2)
select value1, value2
union all
select value1, value2
union all
select value1, value2

Thanks all. 谢谢大家 I loop through and retrieve the inputs as follows . 我遍历并检索输入,如下所示。 I am not sure it its the best way to do it , but it did work for me. 我不确定这是否是最好的方法,但是它确实对我有用。

  private void insert()
        {
            connection.Open();


            for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
            {
                DropDownList DropDownListType =
                         (DropDownList)gvAdditionalDetails.Rows[i].Cells[1].FindControl("DropDownListType");



                TextBox TextBoxDescription =
                (TextBox)gvAdditionalDetails.Rows[i].Cells[i].FindControl("txtDescription");

                string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";

                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@row1", DropDownListType.SelectedItem.Text.Trim());
                cmd.Parameters.AddWithValue("@row2", TextBoxDescription.Text.Trim());
                cmd.Parameters.AddWithValue("@contactID", 39);
                cmd.ExecuteNonQuery();
            }
            connection.Close();
        }

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

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