⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmain.cs

📁 PDA程序PDA for windows CE take a look
💻 CS
📖 第 1 页 / 共 3 页
字号:

            Cmd.Dispose();
            SDR.Dispose();
        
            Cursor.Current = Cursors.Default;
        }

        // 读条形码并查询气瓶信息
        private void btn_ReadBarCode_Click(object sender, EventArgs e)
        {
            IsReceived = -1;
            IsFree = false;
            timerDo.Enabled = false;

            tb_BarCode.Text = "";

            string strBarCode_16 = "";
            string strBarCode_10 = "";
            bool IsRead = RFIDReader.ReadBarCode(ref strBarCode_16, ref strBarCode_10);

            if (IsRead)
            {
                tb_BarCode.Text = strBarCode_10;

                //Sound.PlayWAV(@"\Windows\Infend.wav"); // 播放成功声音

                BarCodeQuery(strBarCode_10);
            }
            else
            {               
                Sound.PlayWAV(@"\Windows\Alarm2.wav"); // 播放失败声音
            }

            IsFree = true;
            timerDo.Enabled = true;
        }

        // 输入条形码编号直接查询气瓶信息
        private void btn_BarCodeQuery_Click(object sender, EventArgs e)
        {
            if (tb_BarCode.Text != "")
            {
                BarCodeQuery(tb_BarCode.Text);
            }
            else
            {
                Sound.PlayWAV(@"\Windows\Alarm2.wav"); 
                MessageBox.Show("请输入条形码编号", "提示");
            }
        }

        // 清空数据
        private void btn_BarCodeClear_Click(object sender, EventArgs e)
        {
            tb_BarCode.Text = "";
            tb_VehicleCode.Text = "";
            tb_ProduceCode.Text = "";
            tb_Cuba.Text = "";
            tb_LastCheckDate.Text = "";
            tb_NextCheckDate.Text = "";
        }

        // 条码输入框获取焦点时 激活输入面板
        private void tb_BarCode_GotFocus(object sender, EventArgs e)
        {
            //inputPanel1.Enabled = true;
        }

        // 条码输入框失去焦点时 禁用输入面板
        private void tb_BarCode_LostFocus(object sender, EventArgs e)
        {
            //inputPanel1.Enabled = false;
        }

        #endregion 读条形码

        #region 数据导入

        // 建数据库和表
        private void CreateDBandTable()
        {
            // 创建数据库文件
            SqlCeEngine engine = new SqlCeEngine(ConnString);
            try
            {  
                engine.CreateDatabase();
            }
            catch (SqlCeException ex)
            {
                Sound.PlayWAV(@"\Windows\Alarm2.wav");

                if (tb_InDataInfo.Text != "")
                {
                    tb_InDataInfo.Text = ex.Message + "\r\n" + tb_InDataInfo.Text;
                }
                else
                {
                    tb_InDataInfo.Text = ex.Message;
                }
            }
            finally
            {
                engine.Dispose();
            }            
            
            // 创建表 
            SqlCeConnection Conn = new SqlCeConnection(ConnString);
            Conn.Open();
            try
            { 
                string strSQL = @"CREATE TABLE GasInfo(车牌号 nvarchar(10), 条码编号 nvarchar(8), 
                        出厂编号 nvarchar(20), 气瓶容积 Decimal(16,2), 上次检验日期 nvarchar(12), 
                        下次检验日期 nvarchar(12), 标签编号 nvarchar(20), 是否已注销 bit)";
                 
                SqlCeCommand SqlCeCmd = new SqlCeCommand(strSQL, Conn);
                SqlCeCmd.ExecuteNonQuery();
                

                // 创建索引 
                strSQL = "CREATE INDEX idx ON GasInfo(条码编号)";
                SqlCeCmd.CommandText = strSQL;
                SqlCeCmd.ExecuteNonQuery();

                // 创建索引 
                strSQL = "CREATE INDEX idx1 ON GasInfo(标签编号)";
                SqlCeCmd.CommandText = strSQL;
                SqlCeCmd.ExecuteNonQuery();

                // 创建索引 
                strSQL = "CREATE INDEX idx2 ON GasInfo(车牌号)";
                SqlCeCmd.CommandText = strSQL;
                SqlCeCmd.ExecuteNonQuery();

                Sound.PlayWAV(@"\Windows\Infend.wav");
                
                string Info = "创建数据库和表成功!";
                if (tb_InDataInfo.Text != "")
                {
                    tb_InDataInfo.Text = Info + "\r\n" + tb_InDataInfo.Text;
                }
                else
                {
                    tb_InDataInfo.Text = Info;
                }
            }
            catch (SqlCeException ex)
            {
                Sound.PlayWAV(@"\Windows\Alarm2.wav");

                if (tb_InDataInfo.Text != "")
                {
                    tb_InDataInfo.Text = ex.Message + "\r\n" + tb_InDataInfo.Text;
                }
                else
                {
                    tb_InDataInfo.Text = ex.Message;
                }
            }
            finally
            {
                Conn.Dispose();     
            }
        }

        // 建库建表
        private void btn_DB_Click(object sender, EventArgs e)
        {
            if (File.Exists(DBPath))
            {
                if (MessageBox.Show("数据库已存在,重建将删除已有数据,确定重建吗?", "提示",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {
                    try
                    {
                        // 先关闭数据连接
                        Conn.Close();
                        // 删除 再创建
                        File.Delete(DBPath);
                        CreateDBandTable();
                        // 打开连接
                        Conn.Open();
                    }
                    catch (IOException ex)
                    {
                        Sound.PlayWAV(@"\Windows\Alarm2.wav");
                        MessageBox.Show(ex.Message, "提示");
                    }
                }
            }
            else
            {
                // 创建数据库和表
                CreateDBandTable();
            }
        }

        // 导入气瓶数据
        private void btn_InData_Click(object sender, EventArgs e)
        {
            int startTick;
            int endTick;
            startTick = Environment.TickCount;

            if (tb_XMLFilePath.Text == "")
            {
                Sound.PlayWAV(@"\Windows\Alarm2.wav");
                MessageBox.Show("请先选择xml文件!", "提示");
                return;
            }

            tb_XMLFilePath.Text = openFileDialog.FileName;

            // 提示信息
            string Info = "正在载入XML文件......";
            tb_InDataInfo.Text = Info;

            // 初始化DataSet
            DataSet ds = new DataSet();

            // 初始化XML架构文件操作流
            FileStream FsXSD = new FileStream(CurrDir + @"\XSD.xsd", FileMode.Open);

            // 初始化XML架构读取设备
            XmlTextReader xtrXSD = new XmlTextReader(FsXSD);

            // 载入XML架构到DS中
            ds.ReadXmlSchema(xtrXSD);

            // 关闭XML架构读取设备
            xtrXSD.Close();

            // 关闭XML架构文件操作流
            FsXSD.Close();

            // 装载了XML架构 载入XML数据到DS
            ds.ReadXml(tb_XMLFilePath.Text);

            // 提示信息
            Info = "正在导入数据,请稍候......";
            tb_InDataInfo.Text = Info + "\r\n" + tb_InDataInfo.Text;

            //SqlCeConnection Conn = new SqlCeConnection(ConnString);
            //Conn.Open();

            SqlCeCommand SqlCeCmd = new SqlCeCommand();
            SqlCeCmd.Connection = Conn;
            
            string VehicleCode, BarCode;         // 车牌号 条码编号 
            string ProduceCode;                  // 出厂编号   
            string Cuba;                         // 气瓶容积
            string LastCheckDate, NextCheckDate; // 上次、下次检验日期
            string TID;                          // 标签编号
            string IsCancel;                     // 是否已注销 0: 未注销 1: 已注销 
            
            // 导入数据数目
            int DataCount = ds.Tables[0].Rows.Count;

            pBar_InData.Value = 0;
            pBar_InData.Maximum = DataCount;

            for (int i = 0; i < DataCount; i++)
            {                
                VehicleCode = ds.Tables[0].Rows[i][0].ToString();
                BarCode = ds.Tables[0].Rows[i][1].ToString();
                ProduceCode = ds.Tables[0].Rows[i][2].ToString();
                Cuba = ds.Tables[0].Rows[i][3].ToString();
                LastCheckDate = Convert.ToDateTime(ds.Tables[0].Rows[i][4].ToString()).ToString("yyyy-MM-dd");
                NextCheckDate = Convert.ToDateTime(ds.Tables[0].Rows[i][5].ToString()).ToString("yyyy-MM-dd");
                TID = ds.Tables[0].Rows[i][6].ToString();
                IsCancel = "0";    // 默认未注销
                
                try
                {                   
                    string strSQL = @"Insert Into GasInfo Values('" + VehicleCode + "','" + BarCode + "','" +
                        ProduceCode + "'," + Cuba + ",'" + LastCheckDate + "','" +
                        NextCheckDate + "','" + TID + "'," + IsCancel + ")";
                    
                    SqlCeCmd.CommandText = strSQL;
                    SqlCeCmd.ExecuteNonQuery();
                }
                //finally
                catch
                {
                }
                // 导入进度提示
                // "导入数据进度:第" + Convert.ToString(i + 1) + "条,共" + Convert.ToString(DataCount) + "条";
                pBar_InData.Value = pBar_InData.Value + 1;
            }

            SqlCeCmd.Dispose();
            ds.Dispose();

            endTick = Environment.TickCount;

            int Tick = endTick - startTick;            

            if (pBar_InData.Value == DataCount)
            {
                Sound.PlayWAV(@"\Windows\Infend.wav");

                Info = "导入数据已完成,耗时" + Tick.ToString() + "毫秒!";
                tb_InDataInfo.Text = Info + "\r\n" + tb_InDataInfo.Text;

                pBar_InData.Value = 0;
                tb_XMLFilePath.Text = "";
            }
            else
            {
                Sound.PlayWAV(@"\Windows\Alarm2.wav");

                Info = "导入数据失败,请重试!";
                tb_InDataInfo.Text = Info + "\r\n" + tb_InDataInfo.Text;

                pBar_InData.Value = 0;
                tb_XMLFilePath.Text = "";
            }
        }

        // 选择xml文件
        private void btn_XMLFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                tb_XMLFilePath.Text = openFileDialog.FileName;
            }
            else
            {
                tb_XMLFilePath.Text = "";
            }
        }

        #endregion 数据导入  
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -