进度条控件
时间:2010-08-21 来源:ha666
  namespace WindowsFormsApplication2
  {
      partial class 读数据库
      {
          /// <summary>
          /// 必需的设计器变量。
          /// </summary>
          private System.ComponentModel.IContainer components = null;
          /// <summary>
          /// 清理所有正在使用的资源。
          /// </summary>
          /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
          protected override void Dispose(bool disposing)
          {
              if (disposing && (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
#region Windows 窗体设计器生成的代码
          /// <summary>
          /// 设计器支持所需的方法 - 不要
          /// 使用代码编辑器修改此方法的内容。
          /// </summary>
          private void InitializeComponent()
          {
              this.progressBar1 = new System.Windows.Forms.ProgressBar();
              this.button1 = new System.Windows.Forms.Button();
              this.label1 = new System.Windows.Forms.Label();
              this.SuspendLayout();
              //
              // progressBar1
              //
              this.progressBar1.Location = new System.Drawing.Point(23, 42);
              this.progressBar1.Name = "progressBar1";
              this.progressBar1.Size = new System.Drawing.Size(686, 23);
              this.progressBar1.TabIndex = 0;
              this.progressBar1.Visible = false;
              //
              // button1
              //
              this.button1.Location = new System.Drawing.Point(621, 110);
              this.button1.Name = "button1";
              this.button1.Size = new System.Drawing.Size(75, 23);
              this.button1.TabIndex = 2;
              this.button1.Text = "读取数据库信息";
              this.button1.UseVisualStyleBackColor = true;
              this.button1.Click += new System.EventHandler(this.button1_Click);
              //
              // label1
              //
              this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                          | System.Windows.Forms.AnchorStyles.Left)
                          | System.Windows.Forms.AnchorStyles.Right)));
              this.label1.AutoSize = true;
              this.label1.Font = new System.Drawing.Font("宋体", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
              this.label1.Location = new System.Drawing.Point(23, 79);
              this.label1.Name = "label1";
              this.label1.Size = new System.Drawing.Size(60, 64);
              this.label1.TabIndex = 3;
              this.label1.Text = "0";
              this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
              //
              // 读数据库
              //
              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
              this.ClientSize = new System.Drawing.Size(731, 167);
              this.Controls.Add(this.label1);
              this.Controls.Add(this.button1);
              this.Controls.Add(this.progressBar1);
              this.MaximizeBox = false;
              this.MinimizeBox = false;
              this.Name = "读数据库";
              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
              this.Text = "Form1";
              this.TopMost = true;
              this.ResumeLayout(false);
              this.PerformLayout();
}
#endregion
          private System.Windows.Forms.ProgressBar progressBar1;
          private System.Windows.Forms.Button button1;
          private System.Windows.Forms.Label label1;
      }
  }
  using System;
  using System.Data;
  using System.Drawing;
  using System.Threading;
  using System.Windows.Forms;
  using System.Data.SqlClient;
  namespace WindowsFormsApplication2
  {
      public partial class 读数据库 : Form
      {
          public 读数据库()
          {
              InitializeComponent();
          }
          private void button1_Click(object sender, EventArgs e)
          {
              label1.Text = "0";
              string sqlstring = "data source=.;database=BBX_RecommendLogDB;uid=sa;pwd=1234567890;";
              SqlConnection connection=new SqlConnection(sqlstring);
              string sql = "select top 400 ProcessDetailID,GoodsTitle,Note from dbo.ProcessDetail";
              SqlCommand cmd=new SqlCommand(sql,connection);
              SqlDataAdapter adp=new SqlDataAdapter();
              adp.SelectCommand = cmd;
              DataSet ds=new DataSet();
              adp.Fill(ds);
              connection.Dispose();
              connection.Close();
              //设置进度条控件属性
              progressBar1.Visible = true;//进度条控件可见
              progressBar1.Minimum = 0;//进度条控件的最小值为0
              progressBar1.Maximum = ds.Tables[0].Rows.Count;//进度条控件的最大值为读取表内容行数
              progressBar1.BackColor = Color.Red;//进度条控件的背景色为红色
              for(int i=0;i<ds.Tables[0].Rows.Count;i++)
              {
                  progressBar1.Value++;
                  Application.DoEvents();
                  label1.Text = ds.Tables[0].Rows[i]["ProcessDetailID"].ToString();
                  Thread.Sleep(100);
              }
              progressBar1.Value = 0;
              progressBar1.Visible = false;
              label1.Text = "0";
          }
      }
  }










