📄 主界面.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 + -