Вы находитесь на странице: 1из 5

Source Code

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace yjProgressBar
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button btnLoadData;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Label lbTime;
private System.Windows.Forms.Timer Clock;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtUnitPrice;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtPName;
private System.Windows.Forms.TextBox txtPId;
public DataSet myDs = new DataSet();

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}

Page 1 of 5
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.btnLoadData = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.label3 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.lbTime = new System.Windows.Forms.Label();
this.Clock = new System.Windows.Forms.Timer(this.components);
this.txtPName = new System.Windows.Forms.TextBox();
this.txtPId = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtUnitPrice = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnLoadData
//
this.btnLoadData.Location = new System.Drawing.Point(248, 224);
this.btnLoadData.Name = "btnLoadData";
this.btnLoadData.Size = new System.Drawing.Size(88, 32);
this.btnLoadData.TabIndex = 1;
this.btnLoadData.Text = "Load Data";
this.btnLoadData.Click += new System.EventHandler(this.btnLoadData_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 265);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(416, 20);
this.statusBar1.TabIndex = 4;
this.statusBar1.Text = "Ready";
//
// label3
//
this.label3.Location = new System.Drawing.Point(88, 104);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 16);
this.label3.TabIndex = 5;
this.label3.Text = "ProductName";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(88, 232);
this.progressBar1.Name = "progressBar1";
this.progressBar1.TabIndex = 0;
//
// lbTime
//

Page 2 of 5
this.lbTime.BackColor = System.Drawing.Color.OliveDrab;
this.lbTime.Font = new System.Drawing.Font("Arial", 12F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.lbTime.ForeColor = System.Drawing.Color.Brown;
this.lbTime.Location = new System.Drawing.Point(8, 232);
this.lbTime.Name = "lbTime";
this.lbTime.Size = new System.Drawing.Size(72, 23);
this.lbTime.TabIndex = 2;
this.lbTime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Clock
//
this.Clock.Enabled = true;
this.Clock.Interval = 1000;
this.Clock.Tick += new System.EventHandler(this.Timer_Tick);
//
// txtPName
//
this.txtPName.Location = new System.Drawing.Point(184, 104);
this.txtPName.Name = "txtPName";
this.txtPName.ReadOnly = true;
this.txtPName.Size = new System.Drawing.Size(168, 20);
this.txtPName.TabIndex = 6;
this.txtPName.Text = "";
//
// txtPId
//
this.txtPId.Location = new System.Drawing.Point(184, 72);
this.txtPId.Name = "txtPId";
this.txtPId.ReadOnly = true;
this.txtPId.Size = new System.Drawing.Size(168, 20);
this.txtPId.TabIndex = 6;
this.txtPId.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(96, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 5;
this.label1.Text = "ProductID";
//
// label4
//
this.label4.Location = new System.Drawing.Point(88, 136);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 16);
this.label4.TabIndex = 5;
this.label4.Text = "UnitPrice";
//
// txtUnitPrice
//
this.txtUnitPrice.Location = new System.Drawing.Point(184, 136);
this.txtUnitPrice.Name = "txtUnitPrice";
this.txtUnitPrice.ReadOnly = true;
this.txtUnitPrice.Size = new System.Drawing.Size(168, 20);

Page 3 of 5
this.txtUnitPrice.TabIndex = 6;
this.txtUnitPrice.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Info;
this.ClientSize = new System.Drawing.Size(416, 285);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label4,
this.label3,
this.txtUnitPrice,
this.txtPName,
this.txtPId,
this.label1,
this.statusBar1,
this.btnLoadData,
this.progressBar1,

this.lbTime});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

//Load the data from the sql database


private void btnLoadData_Click(object sender, System.EventArgs e)
{

String mySql;
String myConnStr;

//progressbar starts working here


statusBar1.Text="Connecting to DB...";
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value =progressBar1.Minimum;
}
for(int i=progressBar1.Minimum; i<=progressBar1.Maximum; i++)
{
progressBar1.PerformStep();

Page 4 of 5
// build the connection string
myConnStr = "User ID=sa; Initial Catalog = jyNWDB;Data
Source=yourdatasource;Password=yourpassword";

// build query string


mySql = "SELECT ProductId, ProductName, UnitPrice FROM Products";

// instantiate the Connection and Command object


SqlConnection myConn = new SqlConnection(myConnStr);
myConn.Open();

SqlDataAdapter myAdapter = new SqlDataAdapter(mySql, myConn);


myAdapter.SelectCommand.CommandText = "SELECT ProductId, ProductName, UnitPrice
FROM Products";
SqlCommandBuilder myBuilder = new SqlCommandBuilder(myAdapter);

//Populate the data table


myAdapter.Fill(myDs, "dtMyProd");
myConn.Close();

//Create the primary key for the data table


DataColumn[] myPK = new DataColumn[1];
myPK[0] = myDs.Tables["dtMyProd"].Columns["ProductId"];
myDs.Tables["dtMyProd"].PrimaryKey = myPK;

DataRow thisRow = myDs.Tables["dtMyProd"].Rows[1];


txtPId.Text = "" + thisRow["ProductId"];
txtPName.Text = (String)thisRow["ProductName"];
txtUnitPrice.Text = "$" + thisRow["UnitPrice"];

statusBar1.Text ="Database loaded";

public string GetTime()


{
string TimeInString="";
int hour=DateTime.Now.Hour;
int min=DateTime.Now.Minute;
int sec=DateTime.Now.Second;

TimeInString=(hour < 10)?"0" + hour.ToString() :hour.ToString();


TimeInString+=":" + ((min<10)?"0" + min.ToString() :min.ToString());
TimeInString+=":" + ((sec<10)?"0" + sec.ToString() :sec.ToString());
return TimeInString;
}

public void Timer_Tick(object sender,EventArgs eArgs)


{
if(sender==Clock)
{
lbTime.Text=GetTime();
}
}
}
}

Page 5 of 5