如何从gridview向数据库插入多行

时间:2013-10-17 07:20:02

标签: asp.net sql-server gridview

我的Gridview有多行文本框和下拉列表...我想用存储过程一次插入多行文本框和下拉列表的数据。怎么可能......请帮助我,

 <asp:gridview ID="Gridview1" runat="server" ShowFooter="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
    <Columns>
     <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="First Name">
          <ItemTemplate>
              <asp:TextBox ID="txtFirstName" runat="server" CssClass="textbox"></asp:TextBox>
          </ItemTemplate>
  </asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
   <ItemTemplate>
      <asp:TextBox ID="txtLastName" runat="server" CssClass="textbox"></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
  <asp:TextBox ID="txtDate" runat="server" CssClass="textbox"></asp:TextBox>
     </ItemTemplate></asp:TemplateField>
   <asp:TemplateField>
  <ItemTemplate>
  <asp:DropDownList ID="ddlAgeRange" runat="server">
   <asp:ListItem Value="-1">Age Range</asp:ListItem>
  </asp:DropDownList>
   </ItemTemplate></asp:TemplateField>
   <asp:TemplateField>
     <ItemTemplate>
   <asp:DropDownList ID="ddlRelationship" runat="server">
    <asp:ListItem Value="-1">Relationship</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate></asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="ddlGender" runat="server">
           <asp:ListItem Value="-1">Gender</asp:ListItem>
           </asp:DropDownList>
       </ItemTemplate></asp:TemplateField>
        </Columns>
         <FooterStyle BackColor="White" ForeColor="#000066" />
         <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
         <RowStyle ForeColor="#000066" />
         <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
         <SortedAscendingCellStyle BackColor="#F1F1F1" />
         <SortedAscendingHeaderStyle BackColor="#007DBB" />
         <SortedDescendingCellStyle BackColor="#CAC9C9" />
         <SortedDescendingHeaderStyle BackColor="#00547E" />
      </asp:gridview>

1 个答案:

答案 0 :(得分:0)

private void userInsert()
{
    if (MessageBox.Show("Do you want to add the new  data ?", "Confirm ", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
    {
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        try
        {
            foreach (GridViewRow dRow in userDataGridView.Rows)
            {
                cmd.CommandText = string.Format(
                    "insert into users(first_name,last_name,default_rate,default_location,spi_user_id,nickname) values('{0}','{1}',{2},{3},{4},'{5}')",
                    userDataGridView.CurrentRow.Cells[0].Value.ToString(),
                    userDataGridView.CurrentRow.Cells[1].Value.ToString(),
                    userDataGridView.CurrentRow.Cells[2].Value.ToString(),
                    locationID2ComboBox.SelectedValue,
                    userDataGridView.CurrentRow.Cells[4].Value.ToString(),
                    userDataGridView.CurrentRow.Cells[5].Value.ToString());
                con.Open();
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("Your data has been added successfully ", "Saved info", ageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
            userSelect();
        }
    }
}