Using foreach for insert gridview data into sql database

前端 未结 5 713
有刺的猬
有刺的猬 2021-01-24 03:34

I have a client side gridview data and I want to insert gridview data to sql database . at first I import data from excel to gridview and now I want to insert it into sql datab

相关标签:
5条回答
  • 2021-01-24 04:00
     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();
                    }
                }
            }
    
    0 讨论(0)
  • 2021-01-24 04:03

    Wow, a Goto....

    Well, you break your foreach after the first record. And after that you start your foreach again from the first one one....

    0 讨论(0)
  • 2021-01-24 04:07

    Try like this ,

    If all the controls are label

    foreach (GridViewRow GVRow in GridView1.Rows)
        {
    
      Label lbl = (Label)GVRow.FindControl("labelID");
    
      string data=lbl.Text;
    
    }
    
    0 讨论(0)
  • 2021-01-24 04:19
    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();
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-24 04:21

    It's taking first record only because there is break written in you foreach loop...

    following is code for you

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

    }

    0 讨论(0)
提交回复
热议问题