【问题标题】:Compare Datagridview and Database field for Update Process比较更新过程的 Datagridview 和数据库字段
【发布时间】:2017-04-26 15:15:16
【问题描述】:

我正在做 winforms.. 我正在比较 datagridview 列值和 db 值..

如果数据库中存在DataGridView 列,如果不存在,我想做更新过程,我想做插入过程。

我试过这段代码

string resultJewelId = null; string QueryJewelId = null;

    private void AddStockTable()
    {
        try
        {
            Sqlcon = objDB.DBConnection();


            QueryJewelId= "Select JewelId from tblStock";

            Sqlcmd = new SqlCommand(QueryJewelId, Sqlcon);


            dr = Sqlcmd.ExecuteReader();

            if (dr.HasRows)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    while (dr.Read())
                    {
                        resultJewelId = dr.GetString(0);

                        if (resultJewelId == dataGridView1[0, i].Value.ToString())
                        {
                            MessageBox.Show("Update process");

                        }
                        else
                        {
                            MessageBox.Show("Insert Process");
                        }
                    }
                } 

            }
        }
        catch (Exception ex)
        { MessageBox.Show(ex.ToString()); }
    }

虽然条件工作正常.. 但我不知道如何在 for 循环中移动下一行值..

请支持我。

感谢和问候

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    执行while (dr.Read()) 语句后,您必须重置DataReader。但唯一的方法是为每一行再次调用此行:

    dr = Sqlcmd.ExecuteReader();

    在你的 while 循环之前。令人难以置信的低效,但它会解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2017-01-28
      • 2016-02-12
      • 1970-01-01
      相关资源
      最近更新 更多