如何将所有gridview数据插入数据库使用while循环 [英] How can insert all gridview data into database useing while loop

查看:93
本文介绍了如何将所有gridview数据插入数据库使用while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for (int y = 0; y < GridView1.Rows.Count - 0; y++)
{
  SqlCommand cmd2 = new SqlCommand("insertDeliveryNote_DetalisTBL2", con2);
 cmd2.CommandType = System.Data.CommandType.StoredProcedure;

 cmd2.Parameters.AddWithValue("@Customer_Id", LabelIdCustomer.Text);
 cmd2.Parameters.AddWithValue("@FK_DeliveryID", txtId.Text);
 cmd2.Parameters.AddWithValue("@Product_Id", GridView1.Rows[y].Cells[1].v);
  cmd2.Parameters.AddWithValue("@Product_Name", GridView1.Rows[y].Cells[2].ToString());
 cmd2.Parameters.AddWithValue("@Price", GridView1.Rows[y].Cells[3].ToString());
  
cmd2.ExecuteNonQuery();





我尝试过:



此代码给出错误 - 将数据类型nvarchar转换为int时出错。

我尝试将所有数据从GridView1插入到数据库之后bind



What I have tried:

This code give me error - "Error converting data type nvarchar to int".
I try to insert all data from GridView1 to database after bind

推荐答案

我在你的另一篇文章中回答了这个问题在gridview中将行转换为整数 [ ^ ]
I have answered this on your other post Convert row to integer in gridview[^]


亲爱的,



以上解决方案(由@ Chill60)将解决您的问题。所以按照他的建议步骤。



--------------

如果有的话然后,gridview中的更多数据,它将调用存储过程的每一行,这可能会导致性能问题。



从下面的链接中,您可以了解如何将整个grdview数据一次性传递给存储过程,而不是每行调用存储过程。



将数据表发送到存储过程 [ ^ ]

将数据表传递给SQL Server 2008中的存储过程 [ ^ ]



请参阅这些链接,它可能会提供想法。
Dear,

The above solution(by @Chill60) will solve your question. So follow the steps what he suggest.

--------------
If there will be more data in gridview then, for every row it will call the stored procedure which may cause of performance issue.

From the below link you can get the idea how you can pass whole grdview data to the stored procedure in one go with out calling stored procedure for each row.

Sending a DataTable to a Stored Procedure[^]
Passing a datatable to a Stored Procedure in SQL Server 2008[^]

Refer these link it may give ideas.


这篇关于如何将所有gridview数据插入数据库使用while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆