c# 开发《Id扫描录入系统》总结

1.项目需求:为公司仓库开发的一个系统,模式一:通过扫描条形码,转换得到id,并可以导出为txt,excel文件。模式二:扫描条形码,上传到指定服务器。

2.项目开发时间:7天。

3.项目涉及技术:一.xml操作(增删改查),二:模拟url post发送请求。

4.界面:一:登录界面:用户名和密码通过xml文件保存和查询。

二:主界面:模式一界面。

三:主界面:模式二界面。

5.相关代码:登录:

public partial class Login : Form
    {
        //定义xmlDoc对象
        private XmlDocument xmlDoc;
        //创建user对象
        Users user = new Users();
        //定义用户等级
        private String degree;

        public Login()
        {
            InitializeComponent();
        }

        //窗体载入
        private void login_Load(object sender, EventArgs e)
        {
            //新建xmlDoc对象
            xmlDoc = new XmlDocument();

            //开发时使用的路径
            //xmlDoc.Load(Path.Combine(Application.StartupPath, "..\\..\\person.xml"));

            //生成安装文件时使用的路径
            xmlDoc.Load(Path.Combine(Application.StartupPath,"..\\user\\person.xml"));
        }

        //取消按钮点击事件
        private void cancel_Click(object sender, EventArgs e)
        {
            username.ResetText();
            password.ResetText();
        }

        //登录按钮点击事件
        private void loginbutton_Click(object sender, EventArgs e)
        {
            //xml文件下所有name的值
            List<String> nameList = new List<string>();

            //xml文件下所有password的值
            List<String> pwdList = new List<string>();

            //xml文件下所有grade的值
            List<String> gradeList = new List<string>();

            //取得根节点
            XmlNode xmlNode=xmlDoc.SelectSingleNode("persons");
            //根节点下的节点集合
            XmlNodeList xmlNodeList = xmlNode.ChildNodes;

            foreach(XmlNode xnf in xmlNodeList)
            {
                //person节点
                XmlElement xmlElement = (XmlElement)xnf;
                //person节点下的子节点集合
                XmlNodeList childList = xmlElement.ChildNodes;
                
                foreach(XmlNode childXml in childList)
                {
                    //<name>123</name>
                    if (childXml.Name=="name")
                    {
                        //name标签的值加入nameList
                        nameList.Add(childXml.InnerText);            
                    }
                    if(childXml.Name=="password")
                    {
                        pwdList.Add(childXml.InnerText);
                    }
                    if (childXml.Name == "grade")
                    {
                        gradeList.Add(childXml.InnerText);
                    }
                }
            }

            //判断用户是否合法
            if (checkUser(nameList, pwdList,gradeList))
            {
                //设置user对象
                user.setName(username.Text);
                user.setPassword(md5(password.Text));
                user.setGrade(degree);

                //转到主界面
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }

        //检测用户是否合法
        private Boolean checkUser(List<String> nameList,List<String> pwdList,List<String> gradeList)
        {
            Boolean b = false;
            String pwd_md5 = md5(password.Text);
            for (int i = 0; i < nameList.Count;i++ )
            {
                if(username.Text==nameList[i]&&pwd_md5==pwdList[i])
                {
                    b = true;
                    degree = gradeList[i];
                }
            }
            return b;
        }

        //监听密码框中的enter按键事件,调用登录按钮事件函数
        private void password_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                this.loginbutton_Click(sender,e);
            }
        }
        //Md5加密
        private String md5(String input)
        {
            Byte[] clearBytes = new UnicodeEncoding().GetBytes(input);
            Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
            return BitConverter.ToString(hashedBytes);
            
        }

    }


主界面处理:

public partial class Main : Form
    {
        //xmlDocument对象
        private XmlDocument xmlDoc;
        //型号对应的替换字符
        private String charValue;
        //定义用户等级
        String grade;
        //新建dataTable对象
        DataTable dt = new DataTable();

        //默认模式为Imei-id
        String modelLibrary = "imeaid";

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {        
            Users user = new Users();
            //显示操作员名字
            userName.Text = user.getName();
            //把等级赋给grade
            grade = user.getGrade();

            //dataGridView初始化
            dataGridViewLoad();

            //模型列表初始
            modelListLoad();
            
        }

        //datagridview初始化
        public void dataGridViewLoad()
        {
            //DataTable dt = new DataTable();
            //设置表头
            dt.Columns.Add("IMEI", System.Type.GetType("System.String"));
            dt.Columns.Add("Id", System.Type.GetType("System.String"));
            dt.Columns.Add("时间", System.Type.GetType("System.String"));
            dt.Columns.Add("操作员", System.Type.GetType("System.String"));

            //DataRow dr = dt.NewRow();
            //dr[0] = "865190019453110";
            //dr[1] = "945311A";
            //dr[2] = "2012-04-17 16:28:36";
            //dr[3] = "lying";
            //dt.Rows.Add(dr);

            datas.DataSource = dt;

            //设置datagridview的属性
            datas.Columns[2].ReadOnly = true;
            datas.Columns[3].ReadOnly = true;
            datas.Columns[0].Width = 120;
            datas.Columns[2].Width = 150;
        }
        
        //型号列表载入
        public void modelListLoad()
        {
            //创建一个xmlDoc对象
            xmlDoc = new XmlDocument();
            //载入xml文件
            //开发时使用的路径
            //xmlDoc.Load(Path.Combine(Application.StartupPath, "..\\..\\model.xml"));
            //生成安装文件时使用的路径
            xmlDoc.Load(Path.Combine(Application.StartupPath,"..\\user\\model.xml"));

            List<String> nameList = new List<string>();
            List<String> valueList = new List<string>();
            //取得根节点
            XmlNode xmlNode = xmlDoc.SelectSingleNode("models");
            //根节点下的节点集合
            XmlNodeList xmlNodeList = xmlNode.ChildNodes;

            foreach (XmlNode xnf in xmlNodeList)
            {
                //model节点
                XmlElement xmlElement = (XmlElement)xnf;
                //model节点下的子节点集合
                XmlNodeList childList = xmlElement.ChildNodes;

                foreach (XmlNode childXml in childList)
                {
                    if (childXml.Name == "name")
                    {
                        nameList.Add(childXml.InnerText);
                    }
                    if (childXml.Name == "value")
                    {
                        valueList.Add(childXml.InnerText);
                    }
                }
            }
            for (int i = 0; i < nameList.Count; i++)
            {
                modelList.Items.Add(new Model(nameList[i], valueList[i]));
            }
            //默认选项0
            modelList.SelectedIndex = 0;
            String v = ((Model)modelList.SelectedItem).Value;

            //MessageBox.Show(model.Value);
            charValue = v;
        }

        //添加型号菜单点击事件
        private void 添加型号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //弹出一个窗体
            AddModel addModel = new AddModel();
            
            //对话框模式显示窗口
            addModel.ShowDialog();

            //更新modelList
            //清除modelList CheckBox
            modelList.Items.Clear();
            //重新载入modeList
            modelListLoad();
        }

        //删除型号菜单点击事件
        private void 删除型号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //弹出一个窗体
            DelModel delModel = new DelModel();
            //对话框模式显示窗口
            delModel.ShowDialog();

            //更新modelList
            //清除modelList CheckBox
            modelList.Items.Clear();
            //重新载入modeList
            modelListLoad();
        }     

        //修改密码菜单点击事件
        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //弹出一个窗体
            UpdatePassword up = new UpdatePassword();
            up.ShowDialog();
        }

        //添加用户菜单点击事件
        private void 添加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //判断权限
            if ("2".Equals(grade))
            {
                AddUser addUser = new AddUser();
                addUser.ShowDialog();
            }
            else
            {
                MessageBox.Show("你无权限添加用户!");
            }
        }

        //用户选择型号改变时的事件
        private void modelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取选择型号对应的替换字符
            String v = ((Model)modelList.SelectedItem).Value;
            //在label中显示
            replaceValue.Text = v;
            //同时赋给成员变量
            charValue = v;
        }

        //因为扫描枪每扫描一串字符后,会有换行,所以监听Enter键
        private void currentInput_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                //判断是入库还是出库
                if (modelLibrary.Equals("imeaid"))
                {
                    //获取字符串
                    String s = currentInput.Text;

                    //判断是否扫描的字符为15位
                    if (s.Length != 0 && s.Length % 15 == 0)
                    {
                        //截取字符串的后15位字符
                        String imea = s.Substring(s.Length - 15, 15);
                        //清除textbox
                        currentInput.Clear();

                        //把截取字符串追加到textbox,这样光标在最后
                        currentInput.AppendText(imea);

                        //id头部
                        String idHead = imea.Substring(8, 6);
                        //id id头部和替换字符组成
                        String id = idHead + charValue;

                        //当前时间
                        String date = DateTime.Now.ToString();

                        //添加新的一行,赋值
                        DataRow dr = dt.NewRow();
                        dr[0] = imea;
                        dr[1] = id;
                        dr[2] = date;
                        dr[3] = userName.Text;
                        dt.Rows.Add(dr);

                        //取消所有选择
                        datas.ClearSelection();
                        //把当前添加的行的第1列高亮选择
                        datas.Rows[datas.Rows.Count - 2].Cells[0].Selected = true;
                        //Console.WriteLine(datas.Rows.Count);

                        //计数    
                        dataCount.Text = Convert.ToString(datas.Rows.Count - 1);
                    }
                    else
                    {
                        MessageBox.Show("扫描出错!");
                    }                 

                }
                else if (modelLibrary.Equals("id"))
                {
                    //获取字符串
                    String s = currentInput.Text;

                    //判断扫描的字符是否为7位
                    //if (s.Length != 0 && s.Length % 7 == 0)
                    //{
                        //截取字符串的后7位字符
                        String imea = s.Substring(s.Length - 7, 7);
                        //清除textbox
                        currentInput.Clear();

                        //把截取字符串追加到textbox,这样光标在最后
                        currentInput.AppendText(imea);

                        //当前时间
                        String date = DateTime.Now.ToString();

                        //添加新的一行,赋值
                        DataRow dr = dt.NewRow();
                        dr[0] = null;
                        dr[1] = imea;
                        dr[2] = date;
                        dr[3] = userName.Text;
                        dt.Rows.Add(dr);

                        //取消所有选择
                        datas.ClearSelection();
                        //把当前添加的行的第1列高亮选择
                        datas.Rows[datas.Rows.Count - 2].Cells[1].Selected = true;
                        //Console.WriteLine(datas.Rows.Count);

                        //计数    
                        dataCount.Text = Convert.ToString(datas.Rows.Count - 1);
                    //}
                    //else
                    //{
                    //    MessageBox.Show("扫描出错!");
                    //}
                    
                }
                
            }
        }

        //保存所有数据Excel格式
        private void saveAll_Click(object sender, EventArgs e)
        {
            if(datas.Rows.Count==0)
            {
                MessageBox.Show("当前没有数据!");
            }
            //SaveFileDialog save = new SaveFileDialog();
            //save.InitialDirectory = "D:";
            //save.Filter = "*.xls|*.*";
            //if (save.ShowDialog() == DialogResult.OK)
            //{
            //    String saveName = save.FileName + ".xls";
               // MessageBox.Show(saveName);
                //创建Excel对象
                Excel.Application excel = new Excel.Application();
                excel.Application.Workbooks.Add(true);
                excel.Visible = true;

                //生成字段名称
                for (int i = 0; i < datas.ColumnCount; i++)
                {
                    excel.Cells[1, i + 1] = datas.Columns[i].HeaderText;
                }

                //填充数据
                //循环行
                for (int j = 0; j < datas.RowCount - 1; j++)
                {
                    //循环列
                    for (int k = 0; k < datas.ColumnCount; k++)
                    {
                        if (datas[k, j].ValueType == typeof(string))
                        {
                            excel.Cells[j + 2, k + 1] = "" + datas.Rows[j].Cells[k].Value.ToString();
                        }
                        else
                        {
                            excel.Cells[j + 2, k + 1] = datas.Rows[j].Cells[k].Value.ToString();
                        }
                    }
                }

                设置禁止弹出保存和覆盖的询问提示框  
                //excel.Visible = false;
                //excel.DisplayAlerts = false;
                //excel.AlertBeforeOverwriting = false;

                保存到临时工作簿
                excel.Application.Workbooks.Add(true).Save();

                保存文件

                //excel.Save("D:" + "\\234.xls");
                //excel.Quit();
            //}
            
        }

        //导出id Excel格式
        private void saveId_Click(object sender, EventArgs e)
        {
            if (datas.Rows.Count == 0)
            {
                MessageBox.Show("当前没有数据!");
            }
            //SaveFileDialog save = new SaveFileDialog();
            //save.InitialDirectory = "D:";
            //save.Filter = "*.xls|*.*";
            //if (save.ShowDialog() == DialogResult.OK)
            //{
            //    String saveName = save.FileName + ".xls";
            // MessageBox.Show(saveName);
            //创建Excel对象
            Excel.Application excel = new Excel.Application();
            excel.Application.Workbooks.Add(true);
            excel.Visible = true;

            //生成字段名称
            
            excel.Cells[1, 1] = datas.Columns[1].HeaderText;
            

            //填充数据
            //循环行
            for (int j = 0; j < datas.RowCount - 1; j++)
            {
                    if (datas[1, j].ValueType == typeof(string))
                    {
                        excel.Cells[j + 2, 1] = "" + datas.Rows[j].Cells[1].Value.ToString();
                    }
                    else
                    {
                        excel.Cells[j + 2, 1] = datas.Rows[j].Cells[1].Value.ToString();
                    }
                
            }

            //设置禁止弹出保存和覆盖的询问提示框  
            //excel.Visible = false;
            //excel.DisplayAlerts = false;
            //excel.AlertBeforeOverwriting = false;

            保存到临时工作簿
            excel.Application.Workbooks.Add(true).Save();

            保存文件

            //excel.Save("D:" + "\\234.xls");
            //excel.Quit();
            //}
        }

        //导出id TXT格式导出
        private void saveIdTxt_Click(object sender, EventArgs e)
        {
            //判断有无数据
            if (datas.Rows.Count == 0)
            {
                MessageBox.Show("当前没有数据!");
            }

            //弹出保存对话框
            SaveFileDialog save = new SaveFileDialog();
            //设置初始目录
            save.InitialDirectory = "D:";
            //设置保存类型的显示
            save.Filter = "*.txt|*.*";

            //点击保存按钮
            if (save.ShowDialog() == DialogResult.OK)
            {
                //得到保存的路径和文件名
                String saveName = save.FileName + ".txt";
                //Console.WriteLine(saveName);

                //新建输出流
                StreamWriter sw = null;
                //传入文件路径,是否追加,以及文本编码
                sw = new StreamWriter(saveName,true,Encoding.ASCII);
                //格式是第一行为空,所以先写入一个空行
                sw.WriteLine();

                //填充数据
                //循环行
                for (int i = 0; i < datas.RowCount - 1; i++)
                {
                    //得到行数据 只有id一列
                   String str=datas.Rows[i].Cells[1].Value.ToString();
                    //写入id
                   sw.WriteLine(str);
                }
                //释放资源
                sw.Dispose();
                //关闭数据流
                sw.Close();

                //提示用户
                MessageBox.Show("数据导出成功");
            }
            
        }

        //
        private void saveAllTxt_Click(object sender, EventArgs e)
        {
            //判断datas里有无数据
            if (datas.Rows.Count == 0)
            {
                MessageBox.Show("当前没有数据!");
            }
            //新建保存文件对话框
            SaveFileDialog save = new SaveFileDialog();
            //设置默认打开目录
            save.InitialDirectory = "D:";
            //设置保存类型
            save.Filter = "*.txt|*.*";

            
            //判断用户点击保存
            if (save.ShowDialog() == DialogResult.OK)
            {
                //获取保存路径和文件名
                String saveName = save.FileName + ".txt";
                //Console.WriteLine(saveName);
                //新建输出流
                StreamWriter sw = null;
                sw = new StreamWriter(saveName, true, Encoding.ASCII);
                String navegation = null;
                //生成字段名称
                for (int i = 0; i < datas.ColumnCount; i++)
                {
                    navegation += datas.Columns[i].HeaderText+"\t\t";
                }

                sw.WriteLine(navegation);

                //填充数据
                //循环行
                for (int j = 0; j < datas.RowCount - 1; j++)
                {
                    //循环列
                    for (int k = 0; k < datas.ColumnCount; k++)
                    {
                        String columnStr=null;
                        //
                        columnStr = datas.Rows[j].Cells[k].Value.ToString();
                        sw.Write(columnStr+"\t");
                    }
                    //每行换行
                    sw.WriteLine();
                }
                //释放资源
                sw.Dispose();
                //关闭数据流
                sw.Close();
                MessageBox.Show("数据导出成功");
            }
        }

        //清空所有数据
        private void deleteAllButton_Click(object sender, EventArgs e)
        {
            //弹出对话框,提示用户是否清空数据
            DialogResult dr = MessageBox.Show(this, "确定清空数据吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            switch (dr)
            {
                case DialogResult.Yes:
                   // datas.DataSource = 0;
                   // datas.DataSource = dt;
                    //datatable清空
                    dt.Clear();
                    //当前录入清空
                    currentInput.Clear();
                    //计数还原
                    dataCount.Text = Convert.ToString(datas.Rows.Count - 1);

                    //提示用户清除成功
                    MessageBox.Show("清除成功!");   
                    //跳出
                    break;
                //用户点击否按钮
                case DialogResult.No:
                    //跳出
                    break;
            }
        }

        //删除数据按钮点击事件
        private void deleteButton_Click(object sender, EventArgs e)
        {
            //判断用户是否选择了行
            if (datas.SelectedRows.Count > 0)
            {
                //弹出提示对话框
                DialogResult dr = MessageBox.Show(this, "确定删除选中的行吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                switch (dr)
                {
                    case DialogResult.Yes:
                        //循环删除用户选择的行(多行删除)
                        for (int i = datas.SelectedRows.Count; i > 0; i--)
                        {
                            datas.Rows.RemoveAt(datas.SelectedRows[i - 1].Index);
                        }

                        //当前录入清空
                        currentInput.Clear();
                        //计数重新设定    
                        dataCount.Text = Convert.ToString(datas.Rows.Count - 1);
                        MessageBox.Show("删除成功!");          
                        break;
                    case DialogResult.No:
                        break;
                }
            }
            //用户没有选择行
            else
            {
                MessageBox.Show("请选择需要删除的行!");
            }
           
        }

        //上传数据
        private void updata_Click(object sender, EventArgs e)
        {
            UpData ud = new UpData();

            //把datas里的数据传到上传窗口中
            String columnStr = null;
            //循环行
            for (int j = 0; j < datas.RowCount - 1; j++)
            {
                //添加到updata窗口中checkedListBox
                columnStr = datas.Rows[j].Cells[1].Value.ToString();
                ud.addValue(columnStr);
            }
            //以对话框模式显示窗口
            ud.ShowDialog();
        }

        //用户点击菜单imea->id模式事件
        private void ImeaIdToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //把模式标记置为imeaid
            modelLibrary = "imeaid";

            //当前模式改为IMEI->Id
            modelLiabraryLabel.Text = "IMEI->Id";
            //上传数据按钮不可见
            updata.Visible = false;

            //两个导出group可见
            groupBox1.Visible = true;
            groupBox2.Visible = true;

            //当前型号、型号列表、替换字符label、替换字符 设为可见
            label2.Visible = true;
            modelList.Visible = true;
            label3.Visible = true;
            replaceValue.Visible = true;

            //datatable清空
            dt.Clear();
            //当前录入清空
            currentInput.Clear();
            //计数    
            dataCount.Text = Convert.ToString(datas.Rows.Count - 1);
        }

        //用户选择id模式菜单点击事件
        private void IdToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //把模式标记置为id
            modelLibrary = "id";

            //当前模式改为Id
            modelLiabraryLabel.Text = "Id";

            //上传数据按钮可见
            updata.Visible = true;
            //两个导出group不可见
            groupBox1.Visible = false;
            groupBox2.Visible = false;

            //当前型号、型号列表、替换字符label、替换字符 设为不可见
            label2.Visible = false;
            modelList.Visible = false;
            label3.Visible = false;
            replaceValue.Visible = false;

            //datatable清空
            dt.Clear();
            //当前录入清空
            currentInput.Clear();
            //计数还原    
            dataCount.Text = Convert.ToString(datas.Rows.Count - 1);
        }
        
    }


上传数据:

public partial class UpData : Form
    {
        private int loginFlag = 0;
        //xmlDocument对象
        private XmlDocument xmlDoc;
        //模型名字列表
        List<String> nameList = new List<string>();
        //模型对应替换字符列表
        List<String> valueList = new List<string>();

        public UpData()
        {
            InitializeComponent();
        }

        //在checkedListBox1中添加一个数据
        public void addValue(String value)
        {
            checkedListBox1.Items.Add(value);
        }

        //窗口载入
        private void UpData_Load(object sender, EventArgs e)
        {
            //设置checkedListBox1中默认选中
            for (int j = 0; j < checkedListBox1.Items.Count; j++)

                checkedListBox1.SetItemChecked(j, true);

            //新建xmlDoc对象
            xmlDoc = new XmlDocument();

            //开发时使用的路径
           // xmlDoc.Load(Path.Combine(Application.StartupPath, "..\\..\\model.xml"));
            //生成安装文件时使用的路径
            xmlDoc.Load(Path.Combine(Application.StartupPath, "..\\user\\model.xml"));

            //取得根节点
            XmlNode xmlNode = xmlDoc.SelectSingleNode("models");
            //根节点下的节点集合
            XmlNodeList xmlNodeList = xmlNode.ChildNodes;

            foreach (XmlNode xnf in xmlNodeList)
            {
                //model节点
                XmlElement xmlElement = (XmlElement)xnf;
                //model节点下的子节点集合
                XmlNodeList childList = xmlElement.ChildNodes;

                foreach (XmlNode childXml in childList)
                {
                    if (childXml.Name == "name")
                    {
                        //把name标签下的值添加到nameList
                        nameList.Add(childXml.InnerText);
                    }
                    if (childXml.Name == "value")
                    {
                        //把value标签下的值添加到valueList
                        valueList.Add(childXml.InnerText);
                    }
                }
            }
            
        }

        //模拟url post发送请求
        private string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            CookieContainer cookie = new CookieContainer();  
            request.CookieContainer = cookie;
            //Stream myRequestStream = request.GetRequestStream();
            //StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            //myStreamWriter.Write(postDataStr);
            //myStreamWriter.Close();
            Stream stream = request.GetRequestStream();  
            byte[] byteArray = Encoding.UTF8.GetBytes(postDataStr);
            stream.Write(byteArray, 0, byteArray.Length);
            stream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            response.Cookies = cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        //登录按钮
        private void button1_Click(object sender, EventArgs e)
        {
            String username = userNameTextBox.Text;
            String password = passwordTextBox.Text;
            String url = urlTextBox.Text;
            String postDataStr = "action=login&name="+username+"&password="+password;

            String result=HttpPost(url,postDataStr);

            if(result.Equals("0"))
            {
                loginFlag = 1;
                MessageBox.Show("登录成功!");
            }
            else if (result.Equals("1"))
            {
                MessageBox.Show("用户名错误!");
            }
            else if (result.Equals("2"))
            {
                MessageBox.Show("密码错误!");
            }
            else
            {
                MessageBox.Show("登录异常");
            }
        }

        //开始上传
        private void button2_Click(object sender, EventArgs e)
        {
            //判断用户是否登录
            if (loginFlag == 0)
            {
                //未登录
                MessageBox.Show("请先登录!");
            }
            else
            {
               // MessageBox.Show("开始上传");
                //action=addDevice&deviceType=&deviceNo=&user=&barcode
                //参数说明:deviceType 设备类型 deviceNo 设备编号 user 用户名 barcode 条码
                //用户名
                String username = userNameTextBox.Text;
                String url = urlTextBox.Text;
                
                string strCollected = null;

                String result = null;

                //上传
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {

                    if (checkedListBox1.GetItemChecked(i))
                    {
                        strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]);
                    }

                    //设备编号
                    String barcode = null;
                    //条码
                    String deviceNo = strCollected;

                    //得到替换字符
                    String type = deviceNo.Substring(6, 1);
                   // Console.WriteLine(type);
                    //根据替换字符查询设备型号
                    String deviceType = null;
                    for (int j = 0; j < valueList.Count;j++ )
                    {
                        if(type.Equals(valueList[j]))
                        {
                            deviceType=nameList[j];
                        }
                        
                    }
                   // Console.WriteLine(deviceType);
                    //Console.WriteLine("******");
                    String postDataStr = "action=addDevice&deviceType=" + deviceType + "&deviceNo=" + deviceNo + "&user=" + username + "&barcode=" + barcode;

                    Console.WriteLine(deviceType + " " + deviceNo + " " + username+" "+barcode);

                    result = HttpPost(url, postDataStr);
                    Console.WriteLine(result);
                    if(!result.Equals("0"))
                    {
                        MessageBox.Show(result +":"+ barcode);
                    }
                }
                
                MessageBox.Show("上传完成!");
            }
        }

        //全选按钮状态改变事件
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                for (int j = 0; j < checkedListBox1.Items.Count; j++)

                    checkedListBox1.SetItemChecked(j, true);
            }
            else
            {
                for (int j = 0; j < checkedListBox1.Items.Count; j++)

                    checkedListBox1.SetItemChecked(j, false);
            }
        }

    }


6.其他功能代码省略。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值