如何解决使用foreach将gridview数据插入到sql数据库中
| 我有一个客户端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;
}
解决方法
它之所以成为第一条记录,只是因为您的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();
}
}
, 哇,后藤...
好吧,您在获得第一条记录后就不敢再做任何事了。然后,您从第一个开始再次开始学习。
, 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,fTechnicalNo)
VALUES (@fName,@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();
}
}
}
, 这样尝试,
如果所有控件都带有标签
foreach (GridViewRow GVRow in GridView1.Rows)
{
Label lbl = (Label)GVRow.FindControl(\"labelID\");
string data=lbl.Text;
}
, 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,fTechnicalNo)
VALUES (@fName,@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();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。