在winform中,如何取得数据读取进度! 提个思路好了while (dataread.read()){.....ProgressBar.Value+=1;} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不知道你说的进度条是不是%多少的那一种.最好不要用它.用想IE那种滚动的条最好,数据取完就完工.我说说我的理由:1)你没有办法精确控制进度,只能是"欺骗"用户,快完成的时候,突然加满,还有好长时间加不满. :)2)取数据的过程本身就是计算密集的过程,最好不要再作其他的密集工作.比如监视取出的进度.如果你是说,工具条如何实现,参考codeproject上的这个: http://www.codeproject.com/cs/miscctrl/progressbar.asp(或者就直接搞个gif文件放到那个地方)最好作出多线程的,参考这个:再谈 Windows 窗体多线程 http://www.microsoft.com/china/msdn/library/dnforms/html/winforms08162002.asp TheAres(班门斧)的方法很有意思,嘿嘿,以后不要告诉我那个程序是你做的哟,不然,呵呵,俺不敢用。别生气哟。 :) To SaSBYa(SaSBYa Green):很多进度显示的都是估计值,包括微软的拷贝文件,下载文件.各种杀毒软件的进度,安装程序的进度.我上学的时候,就一直在考虑如何精确,工作了在知道是假的.没有生气,讨论讨论很好. 我觉得只有一下提取10万条记录以上的记录才需要考虑这个问题(如果有这个需要,不如更改设计)。下面的代码用后台线程实现提取160000多条记录,每提取10000条记录提示提取进度,同时实现分页显示(每页10000),可是用户友好性在这里肯定是以牺牲性能为代价,这个牺牲是值得的。直接拷过去,更改this.sqlConnection1.ConnectionString=...就可以运行了。using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.SqlClient;using System.Threading;namespace WindowsApplication4{ public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; private System.Data.SqlClient.SqlConnection sqlConnection1; private System.Windows.Forms.Button button1; delegate void BindData(); delegate void ShowProgress(int readcount,int total); BindData binddata; ShowProgress showprogress; DataSet ds; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button2; int TotalTable; int current; private System.Windows.Forms.Label label2; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(32, 8); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(456, 200); this.dataGrid1.TabIndex = 0; // // sqlConnection1 // this.sqlConnection1.ConnectionString = "data source=BILLGATES\\NetSDK;initial catalog=Northwind;integrated security=SSPI;p" + "ersist security info=False;workstation id=BILLGATES;packet size=4096"; // // button1 // this.button1.Location = new System.Drawing.Point(496, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(56, 40); this.button1.TabIndex = 1; this.button1.Text = "开始"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.BackColor = System.Drawing.Color.CadetBlue; this.label1.ForeColor = System.Drawing.Color.Brown; this.label1.Location = new System.Drawing.Point(32, 208); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(288, 32); this.label1.TabIndex = 3; // // button3 // this.button3.Location = new System.Drawing.Point(392, 208); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(64, 32); this.button3.TabIndex = 4; this.button3.Text = "下一页"; this.button3.Click += new System.EventHandler(this.button3_Click); // // button2 // this.button2.Location = new System.Drawing.Point(320, 208); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(64, 32); this.button2.TabIndex = 5; this.button2.Text = "上一页"; this.button2.Click += new System.EventHandler(this.button2_Click_1); // // label2 // this.label2.Location = new System.Drawing.Point(464, 216); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(88, 32); this.label2.TabIndex = 6; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(560, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label2, this.button2, this.button3, this.label1, this.button1, this.dataGrid1}); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { binddata=new BindData(BindDataGrid); showprogress=new ShowProgress(BindLabel); } private void treeView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { } private void button1_Click(object sender, System.EventArgs e) { this.button1.Enabled=false; //新建一个后台线程进行数据库查询 ThreadStart ts=new ThreadStart(FillDataGrid); Thread newThread=new Thread(ts); newThread.IsBackground=true; newThread.Start(); } //后台线程 private void FillDataGrid() { ds=new DataSet(); //这将产生10万多条记录 SqlDataAdapter sda=new SqlDataAdapter("select count(OrderID) from [Order Details] cross join Products",this.sqlConnection1); this.sqlConnection1.Open(); int total=(int)sda.SelectCommand.ExecuteScalar(); this.TotalTable=(int)Math.Ceiling(total/10000); int record=10000; int begin=0; sda.SelectCommand.CommandText="select [Order Details].OrderID,Products.ProductName,[Order Details].UnitPrice from [Order Details] cross join Products"; int tableindex=0; while (record<total) { sda.Fill(ds,begin,record,"myTable"+tableindex.ToString()); if (tableindex==0) this.BeginInvoke(binddata); this.BeginInvoke(showprogress,new object[]{record,total}); begin+=10000; record+=10000; tableindex++; } this.BeginInvoke(showprogress,new object[]{total,total}); } private void BindDataGrid() { this.dataGrid1.DataSource=ds; this.dataGrid1.DataMember="myTable0"; this.label2.Text="共"+TotalTable.ToString()+"页,这是第1页"; } private void BindLabel(int readcount,int total) { this.label1.Text="总共"+total.ToString()+"条记录,"+"已读取"+readcount.ToString(); } private void button2_Click(object sender, System.EventArgs e) { MessageBox.Show(ds.Tables[0].Rows.Count.ToString()); } private void button3_Click(object sender, System.EventArgs e) { if (TotalTable==ds.Tables.Count) { current++; this.dataGrid1.DataMember="myTable"+current.ToString(); this.label2.Text="共"+TotalTable.ToString()+"页,这是第"+(current+1).ToString()+"页"; } else if (current<ds.Tables.Count-2) { current++; this.dataGrid1.DataMember="myTable"+current.ToString(); this.label2.Text="共"+TotalTable.ToString()+"页,这是第"+(current+1).ToString()+"页"; } else { } } private void button2_Click_1(object sender, System.EventArgs e) { if (current>0) { current--; this.dataGrid1.DataMembe 忘了一点,sqlConnection1连接Northwind数据库 关于字符串替换的问题 treeview 一个SQL查询语句的问题 大家帮忙看看关于C#问题 C#winform程序引用office组件的clickonce发布 调用sqlserver自带窗体界面 谁能给我讲讲 怎样使DataGrid没有任何数据的时候也显示各栏的列标头? (帖子太多了,就没人看到我的帖子,100分啊)如何把一个不可序列化的对象存入数据库 调用api返回一结构指针?在C#中怎么写?? 怎样写一个系统的需求分析?一般都包括哪些内容呢? c#中有没有二维数组?
1)你没有办法精确控制进度,只能是"欺骗"用户,快完成的时候,突然加满,还有好长时间加不满. :)2)取数据的过程本身就是计算密集的过程,最好不要再作其他的密集工作.比如监视取出的进度.如果你是说,工具条如何实现,参考codeproject上的这个:
http://www.codeproject.com/cs/miscctrl/progressbar.asp
(或者就直接搞个gif文件放到那个地方)最好作出多线程的,参考这个:
再谈 Windows 窗体多线程
http://www.microsoft.com/china/msdn/library/dnforms/html/winforms08162002.asp
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Threading;namespace WindowsApplication4
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Windows.Forms.Button button1;
delegate void BindData();
delegate void ShowProgress(int readcount,int total);
BindData binddata;
ShowProgress showprogress;
DataSet ds;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
int TotalTable;
int current;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(32, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(456, 200);
this.dataGrid1.TabIndex = 0;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=BILLGATES\\NetSDK;initial catalog=Northwind;integrated security=SSPI;p" +
"ersist security info=False;workstation id=BILLGATES;packet size=4096";
//
// button1
//
this.button1.Location = new System.Drawing.Point(496, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 40);
this.button1.TabIndex = 1;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.CadetBlue;
this.label1.ForeColor = System.Drawing.Color.Brown;
this.label1.Location = new System.Drawing.Point(32, 208);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(288, 32);
this.label1.TabIndex = 3;
//
// button3
//
this.button3.Location = new System.Drawing.Point(392, 208);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 32);
this.button3.TabIndex = 4;
this.button3.Text = "下一页";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(320, 208);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 32);
this.button2.TabIndex = 5;
this.button2.Text = "上一页";
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// label2
//
this.label2.Location = new System.Drawing.Point(464, 216);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 32);
this.label2.TabIndex = 6;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(560, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.button2,
this.button3,
this.label1,
this.button1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
binddata=new BindData(BindDataGrid);
showprogress=new ShowProgress(BindLabel);
} private void treeView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
} private void button1_Click(object sender, System.EventArgs e)
{
this.button1.Enabled=false;
//新建一个后台线程进行数据库查询
ThreadStart ts=new ThreadStart(FillDataGrid);
Thread newThread=new Thread(ts);
newThread.IsBackground=true;
newThread.Start();
}
//后台线程
private void FillDataGrid()
{
ds=new DataSet();
//这将产生10万多条记录
SqlDataAdapter sda=new SqlDataAdapter("select count(OrderID) from [Order Details] cross join Products",this.sqlConnection1);
this.sqlConnection1.Open();
int total=(int)sda.SelectCommand.ExecuteScalar();
this.TotalTable=(int)Math.Ceiling(total/10000);
int record=10000;
int begin=0;
sda.SelectCommand.CommandText="select [Order Details].OrderID,Products.ProductName,[Order Details].UnitPrice from [Order Details] cross join Products";
int tableindex=0;
while (record<total)
{
sda.Fill(ds,begin,record,"myTable"+tableindex.ToString());
if (tableindex==0)
this.BeginInvoke(binddata);
this.BeginInvoke(showprogress,new object[]{record,total});
begin+=10000;
record+=10000;
tableindex++;
}
this.BeginInvoke(showprogress,new object[]{total,total});
}
private void BindDataGrid()
{
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="myTable0";
this.label2.Text="共"+TotalTable.ToString()+"页,这是第1页";
} private void BindLabel(int readcount,int total)
{
this.label1.Text="总共"+total.ToString()+"条记录,"+"已读取"+readcount.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
} private void button3_Click(object sender, System.EventArgs e)
{
if (TotalTable==ds.Tables.Count)
{
current++;
this.dataGrid1.DataMember="myTable"+current.ToString();
this.label2.Text="共"+TotalTable.ToString()+"页,这是第"+(current+1).ToString()+"页";
}
else
if (current<ds.Tables.Count-2)
{
current++;
this.dataGrid1.DataMember="myTable"+current.ToString();
this.label2.Text="共"+TotalTable.ToString()+"页,这是第"+(current+1).ToString()+"页";
}
else
{
}
} private void button2_Click_1(object sender, System.EventArgs e)
{
if (current>0)
{
current--;
this.dataGrid1.DataMembe