通过GridView同时填写多行数据新增到数据库,然后在把这些数据都读出并同时更新这些数据的所有列(除ID之外,ID是自增的),这些列的值又都不一样。
例表数据:
用GridView同时新增以下4行数据到数据库,id是自增的。t1、t2都是char类型。
id   t1   t2 
1    aa   aaa
2    bb   bbb
3    cc   ccc
4    dd   ddd然后希望得到的结果是在GridView同时读出这些数据并对这些数据批量更新变成以下数据后更新到数据库???
id   t1   t2 
1    1a   1b
2    2c   2d
3    3e   3f
4    4g   4h
请高手指点~~~~~~~~~~~~~~!!!

解决方案 »

  1.   

    如果想批量新增和更新gridview中的多行数据,常见的做法就是拼接字符串,这种做法性能也是比较好的,示例代码:
    <%@ Page Language="C#" %>      <%@ Import Namespace="System.Text" %>
          <%@ Import Namespace="System.Data.SqlClient" %>      <script runat="server">       void Button1_Click(object sender, EventArgs e)
           {
               StringBuilder query = new StringBuilder();           for (int i = 0; i < GridView1.Rows.Count; i++)
               {               GridViewRow row = GridView1.Rows[i];               string value1 =
          ((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("'","''");               string value2 =
          ((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("'","''");               string value3 = GridView1.DataKeys[i].Value.ToString();               query.Append("UPDATE [Customers] SET [CompanyName] = '")
                      .Append(value1).Append("' , [ContactTitle] = '")
                      .Append(value2).Append("' WHERE [CustomerID] = '")
                      .Append(value3).Append("';\n");
               }           SqlConnection con = new SqlConnection(
          ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);           SqlCommand command = new SqlCommand(query.ToString(), con);
               con.Open();           command.ExecuteNonQuery();           con.Close();
           }       void Page_Load(object sender, EventArgs e)
           {
               if (!Page.IsPostBack)
               {
                   SqlConnection con = new SqlConnection(
          ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);               SqlCommand command = new SqlCommand(
          "SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM
          [Customers]", con);               con.Open();               GridView1.DataSource = command.ExecuteReader();
                   GridView1.DataBind();               con.Close();
               }
           }      </script>      <html xmlns="http://www.w3.org/1999/xhtml" >
          <head runat="server">
           <title>Updating All GridView Rows</title>
          </head>
          <body>
           <form id="form1" runat="server">
           <div>
               <asp:GridView
                    ID="GridView1"
                    Runat="server"
                    DataKeyNames="CustomerID"
                    AutoGenerateColumns="False">
                   <Columns>                  <asp:TemplateField
                           SortExpression="CustomerID"
                           HeaderText="CustomerID">                  <ItemTemplate>                      <asp:TextBox
                               Runat="server"
                               Text='<%# Bind("CustomerID")
                               ID="TextBox1">
                          </asp:TextBox>                  </ItemTemplate>                  </asp:TemplateField>                  <asp:TemplateField
                           SortExpression="CompanyName"
                           HeaderText="CompanyName">                       <ItemTemplate>                          <asp:TextBox
                                  Runat="server"
                                  Text='<%#  Bind("CompanyName") %>'
                                  ID="TextBox2">
                              </asp:TextBox>                      </ItemTemplate>                  </asp:TemplateField>                  <asp:TemplateField
                           SortExpression="ContactName"
                           HeaderText="ContactTitle">                      <ItemTemplate>                          <asp:TextBox
                                  Runat="server"
                                  Text='<%# Bind("ContactTitle") %>'
                                  ID="TextBox3">
                              </asp:TextBox>                      </ItemTemplate>                  </asp:TemplateField>               </Columns>
               </asp:GridView>           <asp:Button
                   ID="Button1"
                   Runat="server"
                   Text="Button"
                   OnClick="Button1_Click" />&nbsp;
             </div>
           </form>
       </body>
       </html>