?? 主界面.cs
字號:
private void groupBox2_Enter(object sender, System.EventArgs e)
{
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void shangji()
{
//判斷機房是否為滿狀態
string strJfNumber=htComputerNum[this.comboBox1.Text].ToString();
if(this.SjNum(this.comboBox1.Text)>=int.Parse(strJfNumber))
{
MessageBox.Show("本機房已滿,不能上機!","計費系統",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
goto shangjiFinish;
}
this.textBox6.Clear();
now=DateTime.Now;
String sql1="insert into 流水帳表 (條形碼,上機時間,上機狀態,上機區域,值班教師) values('"+ this.textBox1.Text +"','"+ now.ToString() +"','1','"+ this.comboBox1.Text +"','"+ frmMain.strTeacher +"')";
SqlCommand cmd=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
numAff=cmd.ExecuteNonQuery();
if(numAff==1)
MessageBox.Show("開始計時!","上機",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.sqlConnection1.Close();
this.textBox1.SelectAll();
shangjiFinish:;
}
private void xiaji()
{
String strPrice;
float floPrice;
int i=this.comboBox1.SelectedIndex;
//獲取上機區域
String sqlSjqy="select 上機區域 from 流水帳表 where 條形碼='"+ this.textBox1.Text +"' and 上機狀態='1'";
SqlCommand cmdSjqy=new SqlCommand(sqlSjqy,this.sqlConnection1);
this.sqlConnection1.Open();
SqlDataReader readerSjqy=cmdSjqy.ExecuteReader();
readerSjqy.Read();
string strSjqy=readerSjqy.GetString(0);
readerSjqy.Close();
this.sqlConnection1.Close();
this.comboBox1.SelectedIndex=this.comboBox1.FindString(strSjqy);
strPrice=this.comboBox1.SelectedValue.ToString();
this.comboBox1.SelectedIndex=i;
floPrice=float.Parse(strPrice);
String strMsg;
String barCode=this.textBox1.Text;
String sql1="select * from 流水帳表 where 條形碼='"+ this.textBox1.Text +"' and 上機狀態='1'";
Decimal bal=Decimal.Parse(this.textBox4.Text); //余額
Decimal cost;//花費
SqlCommand cmd=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
SqlDataReader reader1=cmd.ExecuteReader();
if(reader1.Read())
{
timeSj=reader1.GetDateTime(2);
}
reader1.Close();
this.sqlConnection1.Close();
timeXj=DateTime.Now;
TimeSpan ts=timeXj-timeSj;
//計算花費
float cost1=float.Parse(ts.Minutes.ToString())*floPrice/60+float.Parse(ts.Hours.ToString())*floPrice+float.Parse(ts.Days.ToString())*floPrice*24;
cost=(Decimal)cost1;
int intStrHours=ts.Days*24+ts.Hours;
//提示信息
strMsg="上機"+ intStrHours.ToString() +"小時"+ ts.Minutes.ToString() +"分鐘,花費"+cost.ToString()+"元。";
//計算余額
bal=Decimal.Parse(this.textBox4.Text)-cost;
//更新流水帳表
sql1="update 流水帳表 set 下機時間='"+ timeXj.ToString() +"',花費='"+ cost.ToString() +"',上機狀態='0' where 條形碼='"+ this.textBox1.Text +"' and 上機狀態='1'";
SqlCommand cmd1=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
cmd1.ExecuteNonQuery();
this.sqlConnection1.Close();
//更新帳戶表
sql1="update 帳戶表 set 余額='"+ bal.ToString() +"'where 條形碼='"+ barCode +"'";
SqlCommand cmd2=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
cmd2.ExecuteNonQuery();
this.sqlConnection1.Close();
//更新主界面
this.textBox4.Text=bal.ToString();
this.textBox6.Text=cost.ToString();
//余額不足者,鎖定
if(bal<=0)
{
sql1="update 帳戶表 set 鎖定='1' where 條形碼='"+ barCode +"'";
SqlCommand cmd3=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
cmd3.ExecuteNonQuery();
this.sqlConnection1.Close();
strMsg+="余額不足,賬戶鎖定!";
}
//提示信息框
MessageBox.Show(strMsg,"下機",MessageBoxButtons.OK,MessageBoxIcon.Information);
//激活textBox1
this.textBox1.Focus();
this.textBox1.SelectAll();
}
private bool sjzt()
{
//返回上機人的狀態
bool m;
String sql1="select * from 流水帳表 where 條形碼='"+ this.textBox1.Text +"' and 上機狀態='1'";
SqlCommand cmd=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
SqlDataReader reader1=cmd.ExecuteReader();
m=reader1.Read();
reader1.Close();
this.sqlConnection1.Close();
if(m)
{
return true;
}
else
{
return false;
}
}
private void button6_Click(object sender, System.EventArgs e)
{
this.textBox1.Focus();
DialogResult result=MessageBox.Show("確定要全部下機嗎?","集體下機",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if(result==DialogResult.Cancel)
{
goto finish;
}
//取操作時間,以此時間為下機時間
string strJtxjMsg="%下機統計%\n";
//打開上機流水表
string sqlJtxj="select 條形碼 from 流水帳表 where 上機狀態=1 and 上機區域='"+ this.comboBox1.Text +"'";
SqlCommand cmdJtxj=new SqlCommand(sqlJtxj,this.sqlConnection2);
this.sqlConnection2.Open();
SqlDataReader rdJtxj=cmdJtxj.ExecuteReader();
try
{
while(rdJtxj.Read())
{
//對每一條記錄實施下機操作
DateTime dtNow=DateTime.Now;
string stuBarcode=rdJtxj.GetString(0);
strJtxjMsg+=this.Jtxj(dtNow,stuBarcode);
strJtxjMsg+="\n";
}
if(strJtxjMsg=="%下機統計%\n")
strJtxjMsg="機房已空!";
}
catch(SqlException exe)
{
MessageBox.Show("意外中斷,請管理員處理!");
this.sqlConnection2.Close();
}
finally
{
rdJtxj.Close();
this.sqlConnection2.Close();
MessageBox.Show(strJtxjMsg,"計費系統",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finish:;
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)13)
{
bool i;//判斷用戶是否處于鎖定狀態
sql="select * from 帳戶表 where 條形碼='"+ this.textBox1.Text +"'";
SqlCommand cmd=new SqlCommand(sql,this.sqlConnection1);
this.sqlConnection1.Open();
SqlDataReader reader=cmd.ExecuteReader();
try
{
if(reader.Read())
{
this.textBox2.Text=reader.GetString(1);
this.textBox3.Text=reader.GetString(2);
this.balance=(Decimal)reader.GetSqlDecimal(3);
this.textBox4.Text=this.balance.ToString();
i=reader.GetBoolean(4);
}
else
{
reader.Close();
this.sqlConnection1.Close();
MessageBox.Show("無此用戶,請開戶!","計費系統",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
goto Finish;
}
}
catch(SqlException exe)
{
MessageBox.Show("數據庫出現錯誤!","計費系統",MessageBoxButtons.OK,MessageBoxIcon.Error);
reader.Close();
this.sqlConnection1.Close();
goto Finish;
}
finally
{
reader.Close();
this.sqlConnection1.Close();
}
//string strJfNumber=htComputerNum[this.comboBox1.Text].ToString();
//if(this.SjNum(this.comboBox1.Text)>=int.Parse(strJfNumber))
//{
// MessageBox.Show("本機房已滿,不能上機!","計費系統",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
// goto Finish;
//}
if(i==false)//判斷是否被鎖定
{
//計費模塊
if(this.sjzt())
{
//下機
this.xiaji();
}
else
{
//上機
this.shangji();
}
}
else
{
MessageBox.Show("用戶鎖定,不能上機!","計費系統",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
goto Finish;
}
Finish:this.textBox1.Clear();
this.textBox1.SelectAll();
this.label15.Text=numDqrs(this.comboBox1.Text);
this.label16.Text=numLjrs(this.comboBox1.Text);
}
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//strCurRoom=this.comboBox1.Text;
this.textBox1.Focus();
this.textBox1.SelectAll();
this.label16.Text=numLjrs(this.comboBox1.Text);
this.label15.Text=numDqrs(this.comboBox1.Text);
}
public string Jtxj(DateTime dt,string StuBarcode)//下機操作,本程序中被集體下機按鈕調用
{
DateTime timeXj=dt;
String strPrice;
float floPrice;
strPrice=this.comboBox1.SelectedValue.ToString();
floPrice=float.Parse(strPrice);
String strMsg=StuBarcode+" ";
String barCode=StuBarcode;
Decimal bal=0; //余額
Decimal cost=0;//花費
//讀出帳戶余額
String sql1="select 余額,姓名 from 帳戶表 where 條形碼='"+ barCode +"'";
SqlCommand cmd0=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
SqlDataReader reader0=cmd0.ExecuteReader();
reader0.Read();
bal=(Decimal)reader0.GetSqlDecimal(0);
strMsg+=reader0.GetString(1);
reader0.Close();
this.sqlConnection1.Close();
//讀出上機時間,計算時間差
sql1="select * from 流水帳表 where 條形碼='"+ barCode +"' and 上機狀態='1'";
SqlCommand cmd=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
SqlDataReader reader1=cmd.ExecuteReader();
if(reader1.Read())
{
timeSj=reader1.GetDateTime(2);
//MessageBox.Show(timeSj.ToString());
}
reader1.Close();
this.sqlConnection1.Close();
TimeSpan ts=timeXj-timeSj;
//計算花費
float cost1=float.Parse(ts.Minutes.ToString())*floPrice/60+float.Parse(ts.Hours.ToString())*floPrice+float.Parse(ts.Days.ToString())*floPrice*24;
cost=(Decimal)cost1;
int intSjHour=ts.Days*24+ts.Hours;
//提示信息
strMsg+="上機"+intSjHour.ToString()+"小時"+ ts.Minutes.ToString() +"分鐘,花費"+cost.ToString()+"元 ";
//計算余額
bal=bal-cost;
//更新流水帳表
sql1="update 流水帳表 set 下機時間='"+ timeXj.ToString() +"',花費='"+ cost.ToString() +"',上機狀態='0' where 條形碼='"+ barCode +"' and 上機狀態='1'";
//sql1="update 流水帳表 set 下機時間 ='"+ timeXj.ToString() +"',上機狀態='0' where 條形碼='"+ barCode +"' and 上機狀態='1'";
//MessageBox.Show(sql1);
SqlCommand cmd1=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
cmd1.ExecuteNonQuery();
this.sqlConnection1.Close();
//更新帳戶表
sql1="update 帳戶表 set 余額='"+ bal.ToString() +"'where 條形碼='"+ barCode +"'";
SqlCommand cmd2=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
cmd2.ExecuteNonQuery();
this.sqlConnection1.Close();
//余額不足者,鎖定
if(bal<=0)
{
sql1="update 帳戶表 set 鎖定='1' where 條形碼='"+ barCode +"'";
SqlCommand cmd3=new SqlCommand(sql1,this.sqlConnection1);
this.sqlConnection1.Open();
cmd3.ExecuteNonQuery();
this.sqlConnection1.Close();
strMsg+="余額不足,賬戶鎖定!";
}
//提示信息框
//MessageBox.Show(strMsg,"下機",MessageBoxButtons.OK,MessageBoxIcon.Information);
return strMsg;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -