C# 调用存储过程
缘由: 因做核销系统为了使数据更新更快, 所以设计时把更新及数据联动做成了存储过程. 但是调用时存储过程无法得到实时的进度, 软件执行存储过程有时候一分多钟,
客户端看起来无响应,那么如何做呢.
实现方法: 那么我们可以做一个假的进度条, 让程序在后台执行, 进度条在前台显示,这样更人性化一些. 存储过程完成,进度条也自动停止,显示完成.
废话少说, 先建一个FORM
把 FormBorderStyle = none // 去掉最大化最小化及关闭按钮.
添加控件如下:
1. 两个label //用于显示执行存储过程的影响行数和执行时间
2. 一个Timer // 用于一秒时间更新一下进度条
3.添加进度条
# region from 窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Customs.General
{
public partial class F_progressBar2 : Form
{
int iTime = 0;
bool isEnd = false;
string rows="0";
DataClass.sqlBeans mybeans = new Customs.DataClass.sqlBeans();
public F_progressBar2()
{
InitializeComponent();
}
private void F_progressBar2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 1000;
pargressBarInitalize();
this.btnOk.Enabled = false;
Thread t = new Thread(new ThreadStart(threadProc));
t.Start();
}
private void pargressBarInitalize()
{
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = 100;
// Set the initial value of the ProgressBar.
pBar1.Value = 10;
// Set the Step property to a value of 1 to represent each file being copied.
pBar1.Step = 1;
}
private void timer1_Tick(object sender, EventArgs e)
{
// Display the ProgressBar control.
pBar1.PerformStep();
if (pBar1.Value == 10)
{
pBar1.Value = 1;
}
if (isEnd)
{
//pBar1.Style = ProgressBarStyle.Continuous;
pBar1.Value = 99;
this.lblRows.Text = rows;
this.timer1.Stop();
MessageBox.Show("已经完成!","提示", MessageBoxButtons.OK,MessageBoxIcon.Information );
//btnOk.Enabled = true;
this.Close();
}
else
{
this.lblTime.Text = iTime.ToString();
iTime++;
}
}
public void threadProc()
{
rows = mybeans.getStoredProcedure2("jkMxTo_tb_balance");
isEnd = true ;
// pBar1.Value = 9; //写在此处将会出现交叉的线程.
//this.lblRows.Text = rows;
//MessageBox.Show("已经完成");
//btnOk.Enabled = true;
//this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
#endregion Form 窗体代码结束.
//VS 2008自动生成的代码
InitializeComponent() 等方法块的代码.
namespace Customs.General { partial class F_progressBar2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.resultLabel = new System.Windows.Forms.Label(); this.pBar1 = new System.Windows.Forms.ProgressBar(); this.lblTime = new System.Windows.Forms.Label(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.btnOk = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.lblRows = new System.Windows.Forms.Label(); this.SuspendLayout(); // // resultLabel // this.resultLabel.AutoSize = true; this.resultLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.resultLabel.Location = new System.Drawing.Point(14, 15); this.resultLabel.Name = "resultLabel"; this.resultLabel.Size = new System.Drawing.Size(103, 16); this.resultLabel.TabIndex = 10; this.resultLabel.Text = "执行时间( 秒 ): "; // // pBar1 // this.pBar1.Location = new System.Drawing.Point(12, 46); this.pBar1.Name = "pBar1"; this.pBar1.Size = new System.Drawing.Size(301, 25); this.pBar1.TabIndex = 9; // // lblTime // this.lblTime.AutoSize = true; this.lblTime.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblTime.Location = new System.Drawing.Point(115, 15); this.lblTime.Name = "lblTime"; this.lblTime.Size = new System.Drawing.Size(15, 16); this.lblTime.TabIndex = 10; this.lblTime.Text = "0"; // // timer1 // this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // btnOk // this.btnOk.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnOk.Location = new System.Drawing.Point(238, 112); this.btnOk.Name = "btnOk"; this.btnOk.Size = new System.Drawing.Size(75, 23); this.btnOk.TabIndex = 11; this.btnOk.Text = "确 定"; this.btnOk.UseVisualStyleBackColor = true; this.btnOk.Click += new System.EventHandler(this.btnOk_Click); // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label1.Location = new System.Drawing.Point(193, 15); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(71, 16); this.label1.TabIndex = 10; this.label1.Text = "影响行数:"; // // lblRows // this.lblRows.AutoSize = true; this.lblRows.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblRows.Location = new System.Drawing.Point(270, 15); this.lblRows.Name = "lblRows"; this.lblRows.Size = new System.Drawing.Size(15, 16); this.lblRows.TabIndex = 10; this.lblRows.Text = "0"; // // F_progressBar2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(325, 147); this.Controls.Add(this.btnOk); this.Controls.Add(this.label1); this.Controls.Add(this.lblRows); this.Controls.Add(this.lblTime); this.Controls.Add(this.resultLabel); this.Controls.Add(this.pBar1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "F_progressBar2"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "F_progressBar2"; this.Load += new System.EventHandler(this.F_progressBar2_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label resultLabel; private System.Windows.Forms.ProgressBar pBar1; private System.Windows.Forms.Label lblTime; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Button btnOk; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label lblRows; } }
程序窗体下载路径如下:
http://download.youkuaiyun.com/detail/poloyzhang/4429572
如有问题 poloyzhang@139.com