C# RAM绑码扫描工具

68 篇文章 3 订阅
68 篇文章 1 订阅

 

1.程式执行效果图:

2.Tie_Yards_Input SOURCE CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Web;

namespace Tie_Yards_Input
{
    public class DataBase_Inquire:Form1
    {
        public String server;
        public String database;
        public String uid;
        public String pwd;
        //public String Stored_Procedure;

        public DataBase_Inquire(String SERVER, String DATABASE, String UID, String PWD)
        {
            this.server = SERVER;
            this.database = DATABASE;
            this.uid = UID;
            this.pwd = PWD;
        }

        public Boolean Delete_Items(String Stored_Procedure,String Str)
        {
            Boolean Flag = false;
            String SqlConnectionStr = "server=" + server + ";database=" + database + ";uid=" + uid + ";pwd=" + pwd;
            SqlConnection conn = new SqlConnection(SqlConnectionStr);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(Stored_Procedure,conn);
                cmd.CommandType = CommandType.StoredProcedure;//启动数据库存过程
                cmd.Parameters.Add("@Str", Str);
                cmd.Parameters.Add("@rs",1);//设置返回参数
                cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//启动输出返回
                cmd.ExecuteNonQuery();
                if ((int)cmd.Parameters["@rs"].Value == 0)
                {
                    conn.Close();
                    Flag = true;
                }
                else
                {
                    conn.Close();
                    WriteLog("Connection" + this.server + "Data Base Err!!");
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Flag = false;
                WriteLog(ex.ToString());
            }
            return Flag;
        }
        public Boolean Querying_OrderNumber(String Querying_Str,DataGridView MyDgv,Label MyLab)
        {
            Boolean Flag = false;
            String SqlConnectionStr = "server=" + server + ";database=" + database + ";uid=" + uid + ";pwd=" + pwd;
            SqlConnection conn = new SqlConnection(SqlConnectionStr);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(Querying_Str,conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds,"cs");
                MyDgv.DataSource = ds.Tables[0];
                MyDgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                MyDgv.ReadOnly = true;
                MyDgv.DefaultCellStyle.SelectionBackColor = Color.YellowGreen;
                MyLab.Text = "已查询到数据:" + (MyDgv.Rows.Count - 1).ToString() + "条";
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                WriteLog(ex.ToString());
            }
            return Flag;
        }

        public void WriteLog(String Str)
        {
            FileStream fs = new FileStream("Log.txt",FileMode.Append,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(Str);
            sw.Close();
        }

        public Boolean RAM_TieYards_3(String StoredProcedure_str, String ProductName_Str, String CompleteMachine_SN,
            String BareBoard_SN, String Mac_Address, String NowDate)//插入RAM板3码合一数据
        {
            Boolean Flag = false;
            SqlConnection conn = new SqlConnection("server="+this.server+";database="+this.database+";uid="+this.uid+";pwd="+this.pwd);
            conn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(StoredProcedure_str,conn);//启动存储过程参数
                cmd.CommandType = CommandType.StoredProcedure;//启动储存过程参数
                cmd.Parameters.Add("@ProductName", ProductName_Str);//设置参数1
                cmd.Parameters.Add("@CompleteMachine_SN",CompleteMachine_SN);
                cmd.Parameters.Add("@BareBoard_SN", BareBoard_SN);
                cmd.Parameters.Add("@Mac_Address", Mac_Address);
                cmd.Parameters.Add("@Date_Time", NowDate);
                cmd.Parameters.Add("@rest", 1);//设置返回值
                cmd.Parameters["@rest"].Direction = ParameterDirection.Output;//启动输出返回
                cmd.ExecuteScalar();
                if ((int)cmd.Parameters["@rest"].Value == 0)
                {
                    Flag = true;
                }
                else
                {
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Flag = false;
                ErrLog(ex.ToString());
            }
            return Flag;
        }
        public Boolean QueryingData_Return(List<String> ss,String StoredProcedure)
        {
            Boolean Flag = false;
            String Conn_Str = "server="+this.server+";database="+this.database+";uid="+this.uid+";pwd="+this.pwd;
            SqlConnection conn = new SqlConnection(Conn_Str);
            conn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(StoredProcedure,conn);
                SqlDataReader reader = cmd.ExecuteReader();//读取执行过程
                //List<String> Temp = new List<String>();
                FileStream fs = new FileStream("BOM.log",FileMode.Create,FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                while (reader.Read())
                {
                    String aa = String.Empty;
                    aa = reader[1].ToString() + "," + reader[2].ToString() + "," + reader[3].ToString() + "," + reader[4].ToString();
                    sw.WriteLine(aa);
                    ss.Add(reader[1].ToString());
                    //Temp.Add(aa);
                }
                //ss = Temp;
                sw.Close();
                fs.Close();
                reader.Close();
                conn.Close();
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                this.ErrLog(ex.ToString());
                MessageBox.Show("连接" + this.server + "数据库获取BOM编码信息出错!!", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                System.Environment.Exit(1);
            }
            return Flag;
        }
    }
}
 

3.Form1 SOURCE CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Web;

namespace Tie_Yards_Input
{
    public partial class Form1 : Form
    {
        public List<String> BoxList_Str=new List<string>();
        public List<String> RedCfg = new List<String>();
        public Form1()
        {
            InitializeComponent();
            String strFullPath = Application.ExecutablePath;//读取执行路径
            String strFileName = System.IO.Path.GetFileName(strFullPath);//读取程式名称
            String FileName = strFileName.Substring(0,strFileName.Length-4);
            System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(FileName);
            if (myProcesses.Length > 1)//判断程式是否启动
            {
                MessageBox.Show("程式已启动", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                System.Environment.Exit(0);
            }
            else
            {
                if (ReadCfg("Tit_Yardsd_Input.config") == false)
                {
                    MessageBox.Show("读取Tit_Yardsd_Input.config配置文件出错!!", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Environment.Exit(0);
                }
            }
        }

        public Boolean ReadCfg(String CfgName)
        {
            Boolean Flag = false;
            FileStream fs = new FileStream(CfgName,FileMode.Open,FileAccess.Read);
            StreamReader sr = new StreamReader(fs,Encoding.Default);
            try
            {
                String Temp = String.Empty;
                while ((Temp = sr.ReadLine()) != null)
                {
                    if (Temp[0] != '#')
                    {
                        String[] Array_Str = Temp.Split(new String[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                        RedCfg.Add(Array_Str[1].Trim());
                    }
                }
                Flag = true;
            }
            catch (Exception ex)
            {
                ErrLog(ex.ToString());
                Flag = false;
            }
            return Flag;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String My_StoredProcedure = String.Empty;
            My_StoredProcedure = "SELECT * FROM HUASAN_OrderInfo";
            DataBase_Inquire MyDQ = new DataBase_Inquire("GPUSERVER", "E_OrderInfo", "sa", "adminsystem");
            if (MyDQ.QueryingData_Return(BoxList_Str, My_StoredProcedure) == true)
            {
                foreach (String nn in BoxList_Str)
                    comboBox1.Items.Add(nn);
            }
        }

        public void ErrLog(String Text)
        {
            try
            {
                FileStream fs = new FileStream("Error.log", FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(Text);
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text.Trim() != "" && comboBox1.Text.Trim() != null)
            {
                comboBox1.Enabled = false;
                textBox1.Enabled = true;
                textBox1.Focus();
                textBox1.SelectAll();
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                label5.Text = "";
                if (textBox2.Text.Length == 12&&textBox2.Text.Trim().IndexOf(RedCfg[1].Trim(),StringComparison.OrdinalIgnoreCase)>=0)
                {
                    textBox2.Enabled = false;
                    textBox3.Enabled = true;
                    textBox3.Focus();
                    textBox3.SelectAll();
                }
                else
                {
                    label5.Left = Convert.ToInt32(groupBox3.Width * 0.13);
                    label5.Top = Convert.ToInt32(groupBox3.Height * 0.5);
                    label5.ForeColor = Color.Red;
                    label5.Text = "扫描MAC_Address内容不正确,请重新扫描!!";
                    textBox2.SelectAll();
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                label5.Text = "";
                if (textBox1.Text.Trim().IndexOf(RedCfg[0].Trim(), StringComparison.OrdinalIgnoreCase) >= 0)//判断字符串中是否包含某个字符串
                {
                    textBox1.Enabled = false;
                    textBox2.Enabled = true;
                    textBox2.Focus();
                    textBox2.SelectAll();
                }
                else
                {
                    textBox1.SelectAll();
                    textBox1.Focus();
                    label5.Left = Convert.ToInt32(groupBox3.Width * 0.13);
                    label5.Top = Convert.ToInt32(groupBox3.Height * 0.5);
                    label5.ForeColor = Color.Red;
                    label5.Text = "扫描裸板SN内容不正确,请重新扫描!!";
                }
            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                label5.Text = "";
                if (textBox3.Text.Trim().IndexOf(RedCfg[2].Trim(), StringComparison.OrdinalIgnoreCase) >= 0)//判断字符串中是否包含某个字符串
                {
                    DataBase_Inquire MyDb_In3y = new DataBase_Inquire("GPUSERVER", "E_CompleteMachine","sa","adminsystem");
                    if (MyDb_In3y.RAM_TieYards_3("usp_RAM_TieYards_3_Info", comboBox1.Text.Trim(),
                        textBox3.Text.Trim(), textBox1.Text.Trim(), textBox2.Text.Trim(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) == true)
                    {
                        label5.Left = Convert.ToInt32(groupBox3.Width * 0.13);
                        label5.Top = Convert.ToInt32(groupBox3.Height * 0.5);
                        label5.ForeColor = Color.Green;
                        label5.Text = "";
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox3.Text = "";
                        label5.Text = "扫描的:" + textBox3.Text + "三码合一数据存储成功!!";
                        textBox3.Enabled = false;
                        textBox1.Enabled = true;
                        textBox1.Focus();
                        textBox1.SelectAll();
                    }
                    else
                    {
                        textBox3.SelectAll();
                        textBox3.Focus();
                        label5.Left = Convert.ToInt32(groupBox3.Width * 0.13);
                        label5.Top = Convert.ToInt32(groupBox3.Height * 0.5);
                        label5.ForeColor = Color.Red;
                        label5.Text = "插入RAM_3码三合一数据重复或连接数据库出错!!";
                        //textBox1.Text = "";
                        //textBox2.Text = "";
                        //textBox3.Text = "";
                        textBox3.Enabled = false;
                        textBox1.Enabled = true;
                        textBox1.Focus();
                        textBox1.SelectAll();
                    }      
                }
                else
                {
                    textBox3.SelectAll();
                    textBox3.Focus();
                    label5.Left = Convert.ToInt32(groupBox3.Width * 0.13);
                    label5.Top = Convert.ToInt32(groupBox3.Height * 0.5);
                    label5.ForeColor = Color.Red;
                    label5.Text = "扫描机壳SN内容不正确,请重新扫描!!";
                }
            }
        }
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值