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();
				}
			}
			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(317, 259);
			this.btnLoadData.Name = "btnLoadData";
			this.btnLoadData.Size = new System.Drawing.Size(113, 37);
			this.btnLoadData.TabIndex = 1;
			this.btnLoadData.Text = "加载数据";
			this.btnLoadData.Click += new System.EventHandler(this.btnLoadData_Click);
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 306);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(532, 23);
			this.statusBar1.TabIndex = 4;
			this.statusBar1.Text = "就绪";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(113, 120);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(102, 19);
			this.label3.TabIndex = 5;
			this.label3.Text = "ProductName";
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(113, 268);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(128, 27);
			this.progressBar1.TabIndex = 0;
			// 
			// lbTime
			// 
			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(10, 268);
			this.lbTime.Name = "lbTime";
			this.lbTime.Size = new System.Drawing.Size(92, 27);
			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(236, 120);
			this.txtPName.Name = "txtPName";
			this.txtPName.ReadOnly = true;
			this.txtPName.Size = new System.Drawing.Size(215, 21);
			this.txtPName.TabIndex = 6;
			this.txtPName.Text = "";
			// 
			// txtPId
			// 
			this.txtPId.Location = new System.Drawing.Point(236, 83);
			this.txtPId.Name = "txtPId";
			this.txtPId.ReadOnly = true;
			this.txtPId.Size = new System.Drawing.Size(215, 21);
			this.txtPId.TabIndex = 6;
			this.txtPId.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(123, 83);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(82, 19);
			this.label1.TabIndex = 5;
			this.label1.Text = "ProductID";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(113, 157);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(102, 19);
			this.label4.TabIndex = 5;
			this.label4.Text = "UnitPrice";
			// 
			// txtUnitPrice
			// 
			this.txtUnitPrice.Location = new System.Drawing.Point(236, 157);
			this.txtUnitPrice.Name = "txtUnitPrice";
			this.txtUnitPrice.ReadOnly = true;
			this.txtUnitPrice.Size = new System.Drawing.Size(215, 21);
			this.txtUnitPrice.TabIndex = 6;
			this.txtUnitPrice.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.SystemColors.Info;
			this.ClientSize = new System.Drawing.Size(532, 329);
			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();

			}

			// 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 ="数据库加载完毕";

		}

		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();      
			}
		}
	}
}


