【问题标题】:Using foreach for insert gridview data into sql database使用foreach将gridview数据插入sql数据库
【发布时间】:2011-05-31 09:50:51
【问题描述】:

我有一个客户端 gridview 数据,我想将 gridview 数据插入 sql 数据库。 起初我将数据从 excel 导入到 gridview,现在我想将其插入到 sql 数据库中。

我使用 foreach 循环一一插入记录。但 foreach 循环只选择第一条记录,我不能增加行索引。我怎样才能做到这一点?并选择其他记录?

protected void btnInsertIntoDatabase_Click(object sender, EventArgs e)
    {
    A:
        string Name = string.Empty;
        string CarType = string.Empty;
        string TechnicalNo = string.Empty;
        string ProductionDate = string.Empty;
        string EngaineType = string.Empty;
        string NoInStock = string.Empty;
        string NoForCar = string.Empty;
        string Price = string.Empty;
        string Image = string.Empty;
        string Desc = string.Empty;
        string PartType = string.Empty;
        string Level = string.Empty;
        string Unit = string.Empty;
        string Ratio = string.Empty;
        string Dirham = string.Empty;
        string ExtraMoney = string.Empty;

        int GVCount = GridView1.Rows.Count;

        foreach (GridViewRow GVRow in GridView1.Rows)
        {
            Name = GVRow.Cells[1].Text;
            CarType = GVRow.Cells[2].Text;
            TechnicalNo = GVRow.Cells[3].Text;
            ProductionDate = GVRow.Cells[4].Text;
            EngaineType = GVRow.Cells[5].Text;
            NoInStock = GVRow.Cells[6].Text;
            NoForCar = GVRow.Cells[7].Text;
            Price = GVRow.Cells[8].Text;
            Image = GVRow.Cells[9].Text;
            Desc = GVRow.Cells[10].Text;
            PartType = GVRow.Cells[11].Text;
            Level = GVRow.Cells[12].Text;
            Unit = GVRow.Cells[13].Text;
            Ratio = GVRow.Cells[14].Text;
            Dirham = GVRow.Cells[15].Text;
            ExtraMoney = GVRow.Cells[16].Text;
            break;
        }

        SqlConnection scn = new SqlConnection(clspublic.GetConnectionString());
        SqlCommand scm = new SqlCommand();
        scm.Connection = scn;
        scm.CommandText = @"INSERT INTO tblProduct
                          (fName, fxCarType, fProductionDate, fEngineType, fNoinStock, fNoforCar, fPrice,fRatio,fDirham,fExtraMoney, fImage, fDesc, fxPartType, fxLevel,fUnitType,fTechnicalNo)
               VALUES     (@fName,@fxCarType,@fProductionDate,@fEngineType,@fNoinStock,@fNoforCar,@fPrice,@fRatio,@fDirham,@fExtraMoney,@fImage,@fDesc,@fxPartType,@fxLevel,@fUnitType,@fTechnicalNo)";

        scm.Parameters.AddWithValue("@fName", Name.ToString());
        scm.Parameters.AddWithValue("@fxCarType", CarType.ToString());
        scm.Parameters.AddWithValue("@fTechnicalNo", TechnicalNo.ToString());
        scm.Parameters.AddWithValue("@fProductionDate", ProductionDate.ToString());
        scm.Parameters.AddWithValue("@fEngineType", EngaineType.ToString());
        scm.Parameters.AddWithValue("@fNoinStock", NoInStock.ToString());
        scm.Parameters.AddWithValue("@fNoforCar", NoForCar.ToString());
        scm.Parameters.AddWithValue("@fPrice", Price.ToString());
        scm.Parameters.AddWithValue("@fRatio", Ratio.ToString());
        scm.Parameters.AddWithValue("@fDirham", Dirham.ToString());
        scm.Parameters.AddWithValue("@fExtraMoney", ExtraMoney.ToString());
        scm.Parameters.AddWithValue("@fImage", Image.ToString());
        scm.Parameters.AddWithValue("@fDesc", Desc.ToString());
        scm.Parameters.AddWithValue("@fxPartType", PartType.ToString());
        scm.Parameters.AddWithValue("@fUnitType", Unit.ToString());
        scm.Parameters.AddWithValue("@fxLevel", Level.ToString());

        goto A;
    } 

【问题讨论】:

  • 因为 break 你的代码不起作用检查我为你粘贴的代码....如果你像这样使用 break 你每次只会得到第一条记录

标签: c# asp.net gridview


【解决方案1】:

它取得第一条记录只是因为你的 foreach 循环中写了break...

以下是适合您的代码

    foreach (GridViewRow GVRow in GridView1.Rows)
    {
        Name = GVRow.Cells[1].Text;
        CarType = GVRow.Cells[2].Text;
        TechnicalNo = GVRow.Cells[3].Text;
        ProductionDate = GVRow.Cells[4].Text;
        EngaineType = GVRow.Cells[5].Text;
        NoInStock = GVRow.Cells[6].Text;
        NoForCar = GVRow.Cells[7].Text;
        Price = GVRow.Cells[8].Text;
        Image = GVRow.Cells[9].Text;
        Desc = GVRow.Cells[10].Text;
        PartType = GVRow.Cells[11].Text;
        Level = GVRow.Cells[12].Text;
        Unit = GVRow.Cells[13].Text;
        Ratio = GVRow.Cells[14].Text;
        Dirham = GVRow.Cells[15].Text;
        ExtraMoney = GVRow.Cells[16].Text;


    SqlConnection scn = new SqlConnection(clspublic.GetConnectionString());
 using(con)
 {  
    SqlCommand scm = new SqlCommand();
    scm.Connection = scn;
    scm.CommandText = @"INSERT INTO tblProduct
                      (fName, fxCarType, fProductionDate, fEngineType, fNoinStock, fNoforCar, fPrice,fRatio,fDirham,fExtraMoney, fImage, fDesc, fxPartType, fxLevel,fUnitType,fTechnicalNo)
           VALUES     (@fName,@fxCarType,@fProductionDate,@fEngineType,@fNoinStock,@fNoforCar,@fPrice,@fRatio,@fDirham,@fExtraMoney,@fImage,@fDesc,@fxPartType,@fxLevel,@fUnitType,@fTechnicalNo)";

    scm.Parameters.AddWithValue("@fName", Name.ToString());
    scm.Parameters.AddWithValue("@fxCarType", CarType.ToString());
    scm.Parameters.AddWithValue("@fTechnicalNo", TechnicalNo.ToString());
    scm.Parameters.AddWithValue("@fProductionDate", ProductionDate.ToString());
    scm.Parameters.AddWithValue("@fEngineType", EngaineType.ToString());
    scm.Parameters.AddWithValue("@fNoinStock", NoInStock.ToString());
    scm.Parameters.AddWithValue("@fNoforCar", NoForCar.ToString());
    scm.Parameters.AddWithValue("@fPrice", Price.ToString());
    scm.Parameters.AddWithValue("@fRatio", Ratio.ToString());
    scm.Parameters.AddWithValue("@fDirham", Dirham.ToString());
    scm.Parameters.AddWithValue("@fExtraMoney", ExtraMoney.ToString());
    scm.Parameters.AddWithValue("@fImage", Image.ToString());
    scm.Parameters.AddWithValue("@fDesc", Desc.ToString());
    scm.Parameters.AddWithValue("@fxPartType", PartType.ToString());
    scm.Parameters.AddWithValue("@fUnitType", Unit.ToString());
    scm.Parameters.AddWithValue("@fxLevel", Level.ToString());

   scm.ExecuteNonQuery();
 }

}

【讨论】:

  • 是的,我打破了,因为我有超过 50 条记录,然后我的变量发生了变化。我想注册第一条记录,然后是第二条记录,然后 ....
  • 你看到了吗,他使用了 Go To 语句?
【解决方案2】:

哇,Goto....

好吧,你在第一个记录后打破了你的 foreach。之后你从第一个开始你的 foreach ......

【讨论】:

    【解决方案3】:
     using (SqlConnection scn = new SqlConnection(clspublic.GetConnectionString()))
            {
    
                foreach (GridViewRow GVRow in GridView1.Rows)
                {
                    Name = GVRow.Cells[1].Text;
                    CarType = GVRow.Cells[2].Text;
                    TechnicalNo = GVRow.Cells[3].Text;
                    ProductionDate = GVRow.Cells[4].Text;
                    EngaineType = GVRow.Cells[5].Text;
                    NoInStock = GVRow.Cells[6].Text;
                    NoForCar = GVRow.Cells[7].Text;
                    Price = GVRow.Cells[8].Text;
                    Image = GVRow.Cells[9].Text;
                    Desc = GVRow.Cells[10].Text;
                    PartType = GVRow.Cells[11].Text;
                    Level = GVRow.Cells[12].Text;
                    Unit = GVRow.Cells[13].Text;
                    Ratio = GVRow.Cells[14].Text;
                    Dirham = GVRow.Cells[15].Text;
                    ExtraMoney = GVRow.Cells[16].Text;
    
                    using (SqlCommand scm = scn.CreateCommand())
                    {
    
                        scm.CommandText = @"INSERT INTO tblProduct
                      (fName, fxCarType, fProductionDate, fEngineType, fNoinStock, fNoforCar, fPrice,fRatio,fDirham,fExtraMoney, fImage, fDesc, fxPartType, fxLevel,fUnitType,fTechnicalNo)
           VALUES     (@fName,@fxCarType,@fProductionDate,@fEngineType,@fNoinStock,@fNoforCar,@fPrice,@fRatio,@fDirham,@fExtraMoney,@fImage,@fDesc,@fxPartType,@fxLevel,@fUnitType,@fTechnicalNo)";
    
                        scm.Parameters.AddWithValue("@fName", Name.ToString());
                        scm.Parameters.AddWithValue("@fxCarType", CarType.ToString());
                        scm.Parameters.AddWithValue("@fTechnicalNo", TechnicalNo.ToString());
                        scm.Parameters.AddWithValue("@fProductionDate", ProductionDate.ToString());
                        scm.Parameters.AddWithValue("@fEngineType", EngaineType.ToString());
                        scm.Parameters.AddWithValue("@fNoinStock", NoInStock.ToString());
                        scm.Parameters.AddWithValue("@fNoforCar", NoForCar.ToString());
                        scm.Parameters.AddWithValue("@fPrice", Price.ToString());
                        scm.Parameters.AddWithValue("@fRatio", Ratio.ToString());
                        scm.Parameters.AddWithValue("@fDirham", Dirham.ToString());
                        scm.Parameters.AddWithValue("@fExtraMoney", ExtraMoney.ToString());
                        scm.Parameters.AddWithValue("@fImage", Image.ToString());
                        scm.Parameters.AddWithValue("@fDesc", Desc.ToString());
                        scm.Parameters.AddWithValue("@fxPartType", PartType.ToString());
                        scm.Parameters.AddWithValue("@fUnitType", Unit.ToString());
                        scm.Parameters.AddWithValue("@fxLevel", Level.ToString());
    
                        scm.ExecuteNonQuery();
                    }
                }
            }
    

    【讨论】:

      【解决方案4】:

      这样试试,

      如果所有控件都是标签

      foreach (GridViewRow GVRow in GridView1.Rows)
          {
      
        Label lbl = (Label)GVRow.FindControl("labelID");
      
        string data=lbl.Text;
      
      }
      

      【讨论】:

        【解决方案5】:
        using (SqlConnection scn = new SqlConnection(clspublic.GetConnectionString()))
            {
        
                foreach (GridViewRow GVRow in GridView1.Rows)
                {
                    Name = GVRow.Cells[1].Text;
                    CarType = GVRow.Cells[2].Text;
                    TechnicalNo = GVRow.Cells[3].Text;
                    ProductionDate = GVRow.Cells[4].Text;
                    EngaineType = GVRow.Cells[5].Text;
                    NoInStock = GVRow.Cells[6].Text;
                    NoForCar = GVRow.Cells[7].Text;
                    Price = GVRow.Cells[8].Text;
                    Image = GVRow.Cells[9].Text;
                    Desc = GVRow.Cells[10].Text;
                    PartType = GVRow.Cells[11].Text;
                    Level = GVRow.Cells[12].Text;
                    Unit = GVRow.Cells[13].Text;
                    Ratio = GVRow.Cells[14].Text;
                    Dirham = GVRow.Cells[15].Text;
                    ExtraMoney = GVRow.Cells[16].Text;
        
                    using (SqlCommand scm = scn.CreateCommand())
                    {
        
                        scm.CommandText = @"INSERT INTO tblProduct
                      (fName, fxCarType, fProductionDate, fEngineType, fNoinStock, fNoforCar, fPrice,fRatio,fDirham,fExtraMoney, fImage, fDesc, fxPartType, fxLevel,fUnitType,fTechnicalNo)
           VALUES     (@fName,@fxCarType,@fProductionDate,@fEngineType,@fNoinStock,@fNoforCar,@fPrice,@fRatio,@fDirham,@fExtraMoney,@fImage,@fDesc,@fxPartType,@fxLevel,@fUnitType,@fTechnicalNo)";
        
                        scm.Parameters.AddWithValue("@fName", Name.ToString());
                        scm.Parameters.AddWithValue("@fxCarType", CarType.ToString());
                        scm.Parameters.AddWithValue("@fTechnicalNo", TechnicalNo.ToString());
                        scm.Parameters.AddWithValue("@fProductionDate", ProductionDate.ToString());
                        scm.Parameters.AddWithValue("@fEngineType", EngaineType.ToString());
                        scm.Parameters.AddWithValue("@fNoinStock", NoInStock.ToString());
                        scm.Parameters.AddWithValue("@fNoforCar", NoForCar.ToString());
                        scm.Parameters.AddWithValue("@fPrice", Price.ToString());
                        scm.Parameters.AddWithValue("@fRatio", Ratio.ToString());
                        scm.Parameters.AddWithValue("@fDirham", Dirham.ToString());
                        scm.Parameters.AddWithValue("@fExtraMoney", ExtraMoney.ToString());
                        scm.Parameters.AddWithValue("@fImage", Image.ToString());
                        scm.Parameters.AddWithValue("@fDesc", Desc.ToString());
                        scm.Parameters.AddWithValue("@fxPartType", PartType.ToString());
                        scm.Parameters.AddWithValue("@fUnitType", Unit.ToString());
                        scm.Parameters.AddWithValue("@fxLevel", Level.ToString());
        
                        scm.ExecuteNonQuery();
                    }
                }
            }
        

        【讨论】:

        • 这个问题已经一年多以前回答了。为什么你再次发布几乎相同的代码,没有任何进一步的解释?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-09
        • 1970-01-01
        • 2019-11-22
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多