本文介绍了想在我的数据库和数据网格视图中进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天, 我写了一个代码来更新DataGridView中的单元格,但是在运行异常时发生了 索引超出范围。必须是非负数且小于集合的大小。 参数名称:index 我不知道是什么问题,我的代码有什么问题吗? 注意:我创建了一个DBclass包含所有sqlcommands和i cast当我需要写quires时。 这是我的代码:
private void btn_update_Click( object sender,EventArgs e) { DBClass db = DBClass.GetInstance(); for ( int i = 0 ; i< = dgv_contents.RowCount; i ++) { string str = @ UPDATE [Picture_upload]。[dbo]。[Contents] SET [cont_name] =' + dgv_contents.SelectedRows [ i] .Cells [ 0 ]。值+ ',[cont_type] =' + dgv_contents.Rows [i] .Cells [ 1 ]。值+ ',[Description] =' + dgv_contents.Rows [i] .Cells [ 2 ]。值+ ',[price] = + dgv_contents.Rows [i] .Cells [ 3 ]。值+ ; db.ExecuteNonQuery(str,CommandType.Text); }解决方案
DataGridView的RowCount属性为您提供个行。 但每行的索引从0(零)开始。 因此在你的for循环中 for ( int i = < = 你在DGV的实际行结束时运行。 将其更改为
试试这个代码.. private void btn_update_Click( object sender,EventArgs e) { DBClass db = DBClass.GetInstance (); foreach (DataGridViewRow row in dgv_contents.SelectedRows ) { string str = @ UPDATE [Picture_upload]。[dbo]。[Contents] SET [cont_name] =' + row.Cells [ 0 ]。值+ ',[cont_type] =' + row.Cells [ 1 ]。值+ ',[Description] =' + row.Cells [ 2 ]。值+ ',[price] = + row.Cells [ 3 ] .Value + ; db.ExecuteNonQuery(str,CommandType.Text); } }
这对SQL注入攻击是开放的,即使您的表单不允许这些单元格上的文本框。你应该通过存储过程来做到这一点。你不应该在你的表示层写这么多的DB代码。
Good day all, I wrote a code for updating cells in DataGridView , but while running an Exception occurred "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" I don't know what's the problem ,Is there anything wrong in my code ? Note : i made a DBclass contains all sqlcommands and i cast it when i need to write quires. Here's my Code:
private void btn_update_Click(object sender, EventArgs e) { DBClass db=DBClass.GetInstance(); for(int i=0;i<=dgv_contents.RowCount;i++) { string str = @" UPDATE [Picture_upload].[dbo].[Contents] SET [cont_name] = '"+dgv_contents.SelectedRows[i].Cells[0].Value+ "',[cont_type] ='"+dgv_contents.Rows[i].Cells[1].Value+ "',[Description] ='"+dgv_contents.Rows[i].Cells[2].Value+ "',[price] = "+dgv_contents.Rows[i].Cells[3].Value+""; db.ExecuteNonQuery(str,CommandType.Text); } 解决方案 The RowCount property of a DataGridView gives you the number of rows. But the index of each row starts at 0 (zero). Thus in your for loop for(int i=0;i<=dgv_contents.RowCount;i++)by using <= you are running off the end of actual rows in the DGV. Change it to just be
Try this code.. private void btn_update_Click(object sender, EventArgs e) { DBClass db = DBClass.GetInstance(); foreach (DataGridViewRow row in dgv_contents.SelectedRows) { string str = @" UPDATE [Picture_upload].[dbo].[Contents] SET [cont_name] = '" + row.Cells[0].Value + "',[cont_type] ='" + row.Cells[1].Value + "',[Description] ='" + row.Cells[2].Value + "',[price] = " + row.Cells[3].Value + ""; db.ExecuteNonQuery(str, CommandType.Text); } } This is open to SQL injection attacks, even if your form doesn't allow text boxes on these cells. You should do this via a stored proc. You should NEVER write this much DB code in your presentation layer.更多推荐
想在我的数据库和数据网格视图中进行更新
发布评论