Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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# 填充数据表时获得进展_C#_Database_Progress Bar - Fatal编程技术网

C# 填充数据表时获得进展

C# 填充数据表时获得进展,c#,database,progress-bar,C#,Database,Progress Bar,我必须获取当前存储在DataTable中的记录数量,因为数据库服务器位于其他位置,并且连接速度较慢,所以获取记录有时可能需要一些时间,我想知道下载了多少记录 这是填充数据表的方法: public DataTable fillMyDataTable(string sQuery) { SqlDataAdapter myDataAdapter; SqlCommandBuilder mySqlCommandBuilder; DataTable d

我必须获取当前存储在DataTable中的记录数量,因为数据库服务器位于其他位置,并且连接速度较慢,所以获取记录有时可能需要一些时间,我想知道下载了多少记录

这是填充数据表的方法:

public DataTable fillMyDataTable(string sQuery)
    {

        SqlDataAdapter myDataAdapter;
        SqlCommandBuilder mySqlCommandBuilder;
        DataTable dataTable;

        myDataAdapter = new SqlDataAdapter(sQuery, this.connection);
        mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
        myDataAdapter.SelectCommand.CommandTimeout = 240;

        dataTable = new DataTable();
        mySqlDataAdapter.Fill(dataTable);

        return dataTable;
    }
这是我如何从上面调用方法的: DataTable dt=新的DataTable()

输出应该是2100条记录中的1904条。问题是没有触发dt_RowChanged事件。怎么做

更新
上述代码在触发dt_RowChanged事件之前一直有效,即使dt对象中也充满了记录。

那么您想计算提取的记录数吗

int countFetched = dt.Rows.Count;

要触发事件,请尝试此。如果它没有在事件上使用适当的委托

private void FillData()
{
        string sql = "SELECT Id, Code, Desc FROM myTable";
        dt = fillMyDataTable(sql);
        DataRowChangeEventArgs args = new DataRowChangeEventArgs (dt.Rows, dt.RowState);
        dt_RowChanged(this, args);

}
更新

我只是注意到你没有宣布事件发生了变化,你把它炒了。也许这毕竟是你的错误。一定要再检查一遍

 dt.RowChanged += new DataRowChangeEventHandler(this.dt_RowChanged);

我想我成功了,这是我目前的解决方案:

int iCurrentCount = 0;
int iTotalNumOfRec = 0;


iTotalNumOfRec = getNumOfRec("SELECT COUNT(*) FROM myTable)

dt = fillMyDataTable(sql);           
int i = 0;

      while (i < iTotlNumOfRec)
      {
           DataRowChangeEventArgs args = new DataRowChangeEventArgs(dt.Rows[i], DataRowAction.Add);
           dt_RowChanged(this, args);
           ++i;
      }

     void dt_RowChanged(object sender, DataRowChangeEventArgs e)
     {
          if (e.Action == DataRowAction.Add)
          {               
               Console.WriteLine("Current rec. no.: " + iCurrentCount.ToString());
               iCurrentCount++;
           }
      }
inticcurrentcount=0;
int-iTotalNumOfRec=0;
iTotalNumOfRec=getNumOfRec(“从myTable中选择计数(*))
dt=fillMyDataTable(sql);
int i=0;
而(i

谢谢大家!

那么
dt.Rows.Count
准确地计数怎么样,但我想“实时”地计数“。有时,获取记录会将应用程序阻塞几秒钟或几分钟,具体取决于服务器可用性、获取的记录数量以及何时完成应用程序的解锁。我想避免这种情况。您也可以先在sql中进行计数,比如从myTable中选择计数(iD)。这不需要太多的抓取时间,已经完成了。我想我应该使用后台工作者?我自己从来没有用过,但你应该看看!我对激发dt_RowChanged事件的代码做了一些更改,但evend没有被激发。不知道为什么。第4行有错误:错误1'System.Data.DataRowChangeEventArgs'不包含接受1个参数的构造函数,正如我上面所做的那样。如果失败,那么你可以使用委托。或者让我知道如果失败,我会在我空闲时更新和测试。我已经更改了事件的后期声明,但仍然没有发生任何事情。在您的示例中,DataRowChangeEventArgs args=newDataRowChangeEventArgs(dt.Rows,dt.RowState)仍然失败;dt.RowState属性不存在,因此我已更改为DataRowAction.Add,但在dt.Rows属性上失败
int iCurrentCount = 0;
int iTotalNumOfRec = 0;


iTotalNumOfRec = getNumOfRec("SELECT COUNT(*) FROM myTable)

dt = fillMyDataTable(sql);           
int i = 0;

      while (i < iTotlNumOfRec)
      {
           DataRowChangeEventArgs args = new DataRowChangeEventArgs(dt.Rows[i], DataRowAction.Add);
           dt_RowChanged(this, args);
           ++i;
      }

     void dt_RowChanged(object sender, DataRowChangeEventArgs e)
     {
          if (e.Action == DataRowAction.Add)
          {               
               Console.WriteLine("Current rec. no.: " + iCurrentCount.ToString());
               iCurrentCount++;
           }
      }