Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataTable修改的行数在适配器填充后恢复为0_C#_Ado.net_Datatable - Fatal编程技术网

C# DataTable修改的行数在适配器填充后恢复为0

C# DataTable修改的行数在适配器填充后恢复为0,c#,ado.net,datatable,C#,Ado.net,Datatable,道歉,如果这是一个愚蠢的错误-但已经挣扎了几个星期,这和我没有进一步 问题是-我从基于DT的DS到SQL Db的更新调用只是插入nerw记录,而不是更新修改过的记录 我有一个数据源(httpWebRequested html表),我定期调用它并用它更新属于数据集的数据表。最初,我将其解析为第二个DataTable,并将其与我的DataSet表合并-但这不起作用,因此目前我只有一个DataTable,要么在表中插入新行,要么通过迭代源代码更新现有值,每次向数组中添加一行-检查当前行的存在性,更新或

道歉,如果这是一个愚蠢的错误-但已经挣扎了几个星期,这和我没有进一步

问题是-我从基于DT的DS到SQL Db的更新调用只是插入nerw记录,而不是更新修改过的记录

我有一个数据源(httpWebRequested html表),我定期调用它并用它更新属于数据集的数据表。最初,我将其解析为第二个DataTable,并将其与我的DataSet表合并-但这不起作用,因此目前我只有一个DataTable,要么在表中插入新行,要么通过迭代源代码更新现有值,每次向数组中添加一行-检查当前行的存在性,更新或插入([Session ID]为PK,数据[0]为对应的唯一值):-

在这一点上-计数是精细修改的-拾取属性已更改和添加的行是正确的

当我调用update Db方法时,事情开始出错:-

    public static void updateSqlTable()
    {

        string connectionString = "Connection String here";
        string qry = @"select * from chatData";

        SqlConnection conn = new SqlConnection(connectionString);

        try
        {
            SqlDataAdapter da = new SqlDataAdapter(qry, conn);
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            log.Info("Building Queries...");
            da.UpdateCommand = sb.GetUpdateCommand();
            da.InsertCommand = sb.GetInsertCommand();
            da.DeleteCommand = sb.GetDeleteCommand();

            log.Info("Filling Data into Adapter...");
            int modified = 0;
            int added = 0;
            foreach (DataRow dr in pca.chatDataSetG.Tables[0].Rows)
            {
                if (dr.RowState == DataRowState.Modified)
                {
                    modified++;
                }
                if (dr.RowState == DataRowState.Added)
                {
                    added++;
                }
            }
            //This is where the modified count reverts
            da.Fill(pca.chatDataSetG, "Chat");
            modified = 0;
            added = 0;
            foreach (DataRow dr in pca.chatDataSetG.Tables[0].Rows)
            {
                if (dr.RowState == DataRowState.Modified)
                {
                    modified ++;
                }
                if (dr.RowState == DataRowState.Added)
                {
                    added++;
                }
            }
            conn.Open();
            log.Info("Calling Update to DB...");
            int rowseffected = da.Update(pca.chatDataSetG, "Chat");
            log.Info("Update Complete - " + rowseffected + " rows effected........");

        }
        catch (Exception ex)
        {
            log.Error("Error Updating Db with chat Data", ex);
        }
        finally
        {
            conn.Close();
        }

    }
计数在填充之前,但在填充之后-添加的计数保持不变-好,但修改后的计数变为0-坏:(.我尝试了所有形状,试图看看这里发生了什么-但老实说-我被难住了。任何帮助都将非常非常感谢。Db中的行没有反映新值


Peter

您需要在这行代码中重新填充数据集/表格

//This is where the modified count reverts
da.Fill(pca.chatDataSetG, "Chat");
在MSDN DataAdapter fill中,我认为对所描述的行为有一个解释

您可以对同一数据表多次使用填充方法 主键存在,传入行与匹配行合并 已存在。如果不存在主键,则将传入行追加到 数据表


IMO修改的行与数据库中的原始行合并,因此失去了其行状态,插入的项按说明追加。

您在哪里更新数据库?旁注:您不需要手动计算更改,您可以使用。但是只要调用
DataSet.AcceptChan,gets
将保持不变ges
DataAdapter.Update(DataSet)
(您可以用DataTable替换DataSet)。我正在更新数据库:-conn.Open();log.Info(“调用数据库更新…”);int rowseffect=da.Update(pca.chatDataSetG,“Chat”);这是有道理的,因此它使用Db作为主要源,并用Db值覆盖我的修改-这意味着我的rowseffected永远只等于添加时附加的行数。您能否为我指出一个可能的解决方案-我已尝试从CB one-数据货币问题-但如果行在我调用Update之前没有显示为已修改-那么这并不重要,因为它们无论如何都不在更新范围内。仍然不确定采取什么角度来进行这些行更新。我将尝试..但在此之前,请解释为什么要调用da.Fill(..)再一次-我不明白?!我误解了da.Fill方法的一些基本原理。我只提取了数据-这是第一次推送,我刚刚在那里弹出数据-了解我现在哪里出错了-非常感谢。现在开始了解我为什么会出现货币违规:)如果其他人得到了它,我的select*返回了2个计算列,我也发现了这一点。我做了一个更完整的选择,忽略了这些列,结果很好。阅读优秀的戒掉commandbuilder文章,这篇文章帮助很大-
    public static void updateSqlTable()
    {

        string connectionString = "Connection String here";
        string qry = @"select * from chatData";

        SqlConnection conn = new SqlConnection(connectionString);

        try
        {
            SqlDataAdapter da = new SqlDataAdapter(qry, conn);
            SqlCommandBuilder sb = new SqlCommandBuilder(da);
            log.Info("Building Queries...");
            da.UpdateCommand = sb.GetUpdateCommand();
            da.InsertCommand = sb.GetInsertCommand();
            da.DeleteCommand = sb.GetDeleteCommand();

            log.Info("Filling Data into Adapter...");
            int modified = 0;
            int added = 0;
            foreach (DataRow dr in pca.chatDataSetG.Tables[0].Rows)
            {
                if (dr.RowState == DataRowState.Modified)
                {
                    modified++;
                }
                if (dr.RowState == DataRowState.Added)
                {
                    added++;
                }
            }
            //This is where the modified count reverts
            da.Fill(pca.chatDataSetG, "Chat");
            modified = 0;
            added = 0;
            foreach (DataRow dr in pca.chatDataSetG.Tables[0].Rows)
            {
                if (dr.RowState == DataRowState.Modified)
                {
                    modified ++;
                }
                if (dr.RowState == DataRowState.Added)
                {
                    added++;
                }
            }
            conn.Open();
            log.Info("Calling Update to DB...");
            int rowseffected = da.Update(pca.chatDataSetG, "Chat");
            log.Info("Update Complete - " + rowseffected + " rows effected........");

        }
        catch (Exception ex)
        {
            log.Error("Error Updating Db with chat Data", ex);
        }
        finally
        {
            conn.Close();
        }

    }
//This is where the modified count reverts
da.Fill(pca.chatDataSetG, "Chat");