如何使用C#在Winforms中更新数据库中的DataGridview数据 [英] How To Update The DataGridview Data In DataBase In Winforms Using C#

查看:60
本文介绍了如何使用C#在Winforms中更新数据库中的DataGridview数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码..在Datagridview中只有此内容.仅第一个修改的记录正在更新..其余记录未更新.但是我需要所有已修改的记录都应进行更新.请帮助我.

Here Is My Code..With this In Datagridview Only The First Modified Record Only Updating..Remaining Records are not updating. But I need All The Modified Records Should Be Update. pls help me.

private void btnUpdate(object sender, EventArgs e)
{
using (OleDbConnection connect = new OleDbConnection(connectionString))
            {
                using (OleDbCommand command = new OleDbCommand())
                {
                    command.Connection = connect;
                    command.CommandText = "update customer set Name1=@name,Address=@address where Name1=@name";

                    command.Parameters.Add(new OleDbParameter("name", OleDbType.VarChar));
                    command.Parameters.Add(new OleDbParameter("address", OleDbType.VarChar));
                    connect.Open();
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (!row.IsNewRow)
                        {
                            command.Parameters["name"].Value = row.Cells[0].Value;
                            command.Parameters["address"].Value = row.Cells[1].Value;
                            command.ExecuteNonQuery();
                        }
                    }

                }
            }
}

推荐答案

1)您是否在foreach (DataGridViewRow row in dataGridView1.Rows)行上设置了断点并逐步执行代码以确保您甚至尝试编写更新超过一排?
2)您应该将查询稍微更改为update customer set Address=@address where Name1=@name或使用其他列作为行选择器.
1) Have you set a breakpoint on the foreach (DataGridViewRow row in dataGridView1.Rows) row and stepped through the code to ensure that you are even attempting to write updates for more than one row?
2) You should change your query a bit to update customer set Address=@address where Name1=@name or use a different column as the row selector.


这篇关于如何使用C#在Winforms中更新数据库中的DataGridview数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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