📄 fmain.cs.svn-base
字号:
e.KeyCode == Keys.Down)
{
//设置按音
Scanning.Beep(60,1000);
}
}
private void myMenu2_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up ||
e.KeyCode == Keys.Down)
{
//设置按音
Scanning.Beep(60,1000);
}
}
private void p_About_KeyPress(object sender, KeyPressEventArgs e)
{
this.p_About.Visible = false;
this.myMenu1.Visible = true;
this.myMenu1.Focus();
}
private void FMain_Activated(object sender, EventArgs e)
{
if(this.FocusCtr != null)
{
this.Focus();
this.FocusCtr.Focus();
}
}
private void FMain_Deactivate(object sender, EventArgs e)
{
if(this.FocusCtr != null)
{
this.Focus();
this.FocusCtr.Focus();
}
}
private void myMenu1_GotFocus(object sender, EventArgs e)
{
FocusCtr = (Control)sender;
}
private void myMenu2_GotFocus(object sender, EventArgs e)
{
FocusCtr = (Control)sender;
}
private void p_About_GotFocus(object sender, System.EventArgs e)
{
FocusCtr = (Control)sender;
}
#endregion
#region 资产盘查代码
private void t_CheckBarCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Escape)
{
this.p_Check.Visible = false;
this.myMenu1.Visible = true;
this.myMenu1.Focus();
}
if(e.KeyCode == Keys.Down)
{
this.t_ContentInfo.Select(this.t_ContentInfo.Text.Length, 0);
this.t_ContentInfo.ScrollToCaret();
this.t_CheckBarCode.Focus();
}
if(e.KeyCode == Keys.Up)
{
this.t_ContentInfo.Select(1, 0);
this.t_ContentInfo.ScrollToCaret();
this.t_CheckBarCode.Focus();
}
if(e.KeyCode == Keys.Enter)
{
CheckData();
}
}
private void t_CheckBarCode_GotFocus(object sender, System.EventArgs e)
{
FocusCtr = (Control)sender;
//开始扫描
if ( !scan.MyReaderData.IsPending )
scan.StartRead();
}
private void t_CheckBarCode_LostFocus(object sender, System.EventArgs e)
{
//停止扫描
scan.StopRead();
}
private void scan_Scanned(object sender, string e)
{
this.t_CheckBarCode.Text = e;
CheckData();
//开始扫描
if ( !scan.MyReaderData.IsPending )
scan.StartRead();
}
private void CheckData()
{
string BarCode = ""; //扫描的条码
BarCode = this.t_CheckBarCode.Text.Trim();
//判断扫描的条码是否为空
if(BarCode.Equals(string.Empty))
{
MessageBox.Show("请扫描或输入条码!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
this.t_CheckBarCode.Focus();
this.t_CheckBarCode.SelectAll();
return;
}
try
{
string sql = ""; //SQL语句
SqlCeCommand sqlCeCommand = null;
SqlCeDataReader dataReader = null;
sqlCeCommand = conn.CreateCommand();
sql = @"SELECT * FROM 基本资料 WHERE 资产编号='" + BarCode + "'";
sqlCeCommand.CommandText = sql;
dataReader = sqlCeCommand.ExecuteReader();
//判断扫描的条码在基本资料中是否存在
if(dataReader.Read())
{
this.t_ContentInfo.Text = "";
this.t_ContentInfo.Text += "资产名称:" + dataReader["资产名称"].ToString();
this.t_ContentInfo.Text += "\r\n规格型号:" + dataReader["规格型号"].ToString();
this.t_ContentInfo.Text += "\r\n使用部门:" + dataReader["使用部门"].ToString();
this.t_ContentInfo.Text += "\r\n购置日期:" + dataReader["购置日期"].ToString();
this.t_ContentInfo.Text += "\r\n安装存放地:" + dataReader["安装存放地"].ToString();
this.t_ContentInfo.Text += "\r\n责任人:" + dataReader["责任人"].ToString();
this.t_ContentInfo.Text += "\r\n设备状态:" + dataReader["设备状态"].ToString();
this.t_ContentInfo.Text += "\r\n使用状况:" + dataReader["使用状况"].ToString();
sql = "SELECT * FROM 盘查资料表 WHERE 资产编号='" + BarCode + "'";
sqlCeCommand.CommandText = sql;
dataReader = sqlCeCommand.ExecuteReader();
if(!dataReader.Read())
{
sqlCeCommand.CommandText = "INSERT INTO 盘查资料表(资产编号, 盘查日期) VALUES('" + BarCode + "','" + DateTime.Now.ToShortDateString() + "')";
sqlCeCommand.ExecuteNonQuery();
this.TotalCount(); //统计已盘查的资产数量
}
else
{
MessageBox.Show("该固定资产已经盘查过了!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
}
}else{
MessageBox.Show("基本资料中没有该固定资产!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
this.t_ContentInfo.Text = "";
}
sqlCeCommand.Dispose();
dataReader.Close();
dataReader.Dispose();
}
catch
{
MessageBox.Show("出错了,系统未能完成本次操作!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
}
this.t_CheckBarCode.Focus();
this.t_CheckBarCode.SelectAll();
}
private void TotalCount()
{
string sql;
try
{
SqlCeCommand sqlCeCommand = conn.CreateCommand();
SqlCeDataReader dataReader = null;
sql = @"SELECT Count(*) AS total FROM 盘查资料表";
sqlCeCommand.CommandText = sql;
dataReader = sqlCeCommand.ExecuteReader();
if(dataReader.Read())
{
this.l_TotalCount.Text = "ESC返回 盘查数量:" + dataReader["total"].ToString();
}
dataReader.Close();
dataReader.Dispose();
sqlCeCommand.Dispose();
}
catch(System.Data.SqlServerCe.SqlCeException ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 数据转输代码
private void myMenu2_MenuSelected(object sender, MenuItem e)
{
//返回主菜单
if(e.Equals(this.menuItem7))
{
this.myMenu2.Visible = false;
this.myMenu1.Visible = true;
this.myMenu1.Focus();
}
//导入数据
if(e.Equals(this.menuItem5))
{
DataIn();
this.myMenu2.Focus();
}
//导出数据
if(e.Equals(this.menuItem6))
{
DataOut();
this.myMenu2.Focus();
}
}
private void DataIn()
{
//指定导入文件的路径及名称
string filename = @"\MY Documents\基础资料.txt";
if(!File.Exists(filename))
{
MessageBox.Show("导入的文件 " + filename + " 没有找到!");
return;
}
//在导入数据前清除原有的数据
if(MessageBox.Show("导入新的数据后,原有的数据将会被清除!\n是否要继续导入数据呢?","提醒", MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) == DialogResult.No)
{
return;
}
this.l_Load.Text = "正在清除原有数据,请稍后...";
this.p_Load.Location = new Point(16, 80);
this.p_Load.Visible = true;
this.Update();
try
{
SqlCeCommand sqlCommand = conn.CreateCommand();
sqlCommand.CommandText = "DELETE FROM 基本资料";
sqlCommand.ExecuteNonQuery();
sqlCommand.CommandText = "DELETE FROM 盘查资料表";
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
catch
{
MessageBox.Show("系统出现异常,资料清除失败!请使用[主菜单]中的[清除数据]来清除数据!");
this.p_Load.Visible = false;
return;
}
StreamReader sr = null;
try
{
sr = new StreamReader(filename,System.Text.Encoding.GetEncoding("GB2312"));
string linetext;
string[] lineFields;
long n = 0; //用来记录导入的记录条数
this.l_Load.Text = "数据导入中,请稍后...";
this.p_Load.Location = new Point(16, 80);
this.p_Load.Visible = true;
this.Update();
SqlCeCommand sqlCeCommand = conn.CreateCommand();
while ((linetext = sr.ReadLine()) != null)
{
if (linetext.Trim().Equals(string.Empty))
{
continue;
}
lineFields = linetext.Split(new char[] {'\t'});
sqlCeCommand.CommandText ="insert into 基本资料(资产编号, 资产名称, 规格型号, 使用部门, 购置日期, 安装存放地, 责任人, 设备状态, 使用状况) values('"
+ left(lineFields[0], 20) + "','"
+ left(lineFields[1], 50) + "','"
+ left(lineFields[2], 50) + "','"
+ left(lineFields[3], 20) + "','"
+ left(lineFields[4], 10) + "','"
+ left(lineFields[5], 20) + "','"
+ left(lineFields[6], 10) + "','"
+ left(lineFields[7], 3) + "','"
+ left(lineFields[8], 3) + "')";
sqlCeCommand.ExecuteNonQuery();
n ++;
this.l_Load.Text = n.ToString() + " 条数据已经被导入...";
this.p_Load.Update();
}
sr.Close();
sqlCeCommand.Dispose();
GuangMa.Scanning.Beep(60,2670);
MessageBox.Show(n.ToString() + " 条数据导入成功!");
}
catch
{
sr.Close();
MessageBox.Show("导入数据失败,可能是导入数据的格式不正确!");
}
this.p_Load.Visible = false;
}
private void DataOut()
{
//指定导出文件的路径及名称
string filename = @"\MY Documents\盘查资料.txt";
if(File.Exists(filename))
{
if(MessageBox.Show("导出的文件夹[" + filename + "]已经存在,是否要复盖该文件!","提醒", MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) == DialogResult.No)
{
return;
}
}
try
{
StreamWriter sw = new StreamWriter(filename);
string linetext;
long n = 0; //用来记录导出的记录条数
this.l_Load.Text = "正在准备导出数据,请稍后...";
this.p_Load.Text = filename;
this.p_Load.Location = new Point(16, 80);
this.p_Load.Visible = true;
this.Update();
SqlCeCommand sqlCeCommand = conn.CreateCommand();
sqlCeCommand.CommandText = "SELECT * FROM 盘查资料表";
SqlCeDataReader dataReader = sqlCeCommand.ExecuteReader();
while (dataReader.Read())
{
//以TAB键作为分隔符写入文件
linetext = dataReader["资产编号"].ToString() + "\t" + dataReader["盘查日期"].ToString();
sw.WriteLine(linetext);
n ++;
this.l_Load.Text = n.ToString() + " 条记录已经导出...";
this.p_Load.Update();
}
this.p_Load.Visible = false;
sw.Close();
sqlCeCommand.Dispose();
GuangMa.Scanning.Beep(60,2670);
MessageBox.Show(n.ToString() + " 条数据导出成功!");
}
catch(Exception ex)
{
MessageBox.Show("数据导出失败!原因如下:\n" + ex.Message.ToString());
}
this.p_Load.Visible = false;
}
private string left(string s, int l)
{
string result = "";
if(s.Length > l)
{
result = s.Substring(0,l);
}
else
{
result = s;
}
result = getString(result);
return result;
}
private string getString(string p)
{
string result = "";
result = p.Replace("'", "''");
return result;
}
#endregion
#region 清除数据代码
private void ClearData()
{
try
{
if(MessageBox.Show("您是否要清除数据,如果按[是]将清除系统中的所有数据!","提醒", MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
conn.Close(); //关闭数据库连接
File.Delete(SqlCeManage.DatabaseName); //删除数据库文件
SqlCeManage.InitialDatabase(); //重新初始化数据库
conn.Open(); //重新打开数据库连接
MessageBox.Show("数据清除成功!");
}
}
catch
{
MessageBox.Show("系统出现异常,清除数据失败!");
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -