将多个记录插入sql server

时间:2013-09-24 03:58:08

标签: c# sql sql-server insert

伙计我有一个网格,需要从中检索文本输入,然后插入数据库。网格如下所示

从上面它允许用户根据需要将尽可能多的行传递给数据库。我使用下面的方法。

   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();
        }

上面的方法循环抛出我的网格,如何为具有下拉选择和具有tetxbox控件的描述的列类型返回null。我无法调用此控件,因为它们是在网格中声明的。如何从下拉列表中检索所选项目的文本以及从文本框中插入的文本。代码gvAdditionalDetails.Rows[i].Cells[1].Text.Trim()返回null。

4 个答案:

答案 0 :(得分:3)

您需要引用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();
        }

答案 1 :(得分:2)

INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )

答案 2 :(得分:0)

INSERT INTO Table (Column1, Column2)
select value1, value2
union all
select value1, value2
union all
select value1, value2

答案 3 :(得分:0)

谢谢大家。我循环并检索输入如下。我不确定它是最好的方法,但它对我有用。

  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();
        }