Example #1
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         if (txtpwd.Text.Trim()==""||comboBox1.Text=="")
         {
             MessageBox.Show("�û��������벻��Ϊ�գ�", "��ʾ",//������Ϣ�Ի���
                 MessageBoxButtons.OK, MessageBoxIcon.Information);
             return;//�˳��¼�
         }
         else
         {
             string name = comboBox1.Text;//��ȡ�û���
             string pwd = txtpwd.Text.Trim();//��ȡ����
             SqlConnection conn = DBConnection.MyConnection();//�������ݿ����Ӷ���
             conn.Open();//�����ݿ�����
             SqlCommand cmd = new SqlCommand(//�������ݿ��������
                 "select * from HPES_user where name='" + name + "' and password='******'", conn);
             SqlDataReader sdr = cmd.ExecuteReader();//�������ݶ�ȡ��
             sdr.Read();//��ȡ����
             if (sdr.HasRows)//�ж��Ƿ�������
             {
                 string time = DateTime.Now.ToString();//�õ�ʱ����Ϣ
                 string sql = //����SQL�ַ���
                     "update HPES_user set logintime='" + time + "' where name='" + name + "'";
                 operate.OperateData(sql);//�������ݿ���Ϣ
                 this.MdiParent.Close();//�رո�����
                 conn.Close();//�ر�����
                 frmMain Main = new frmMain();//�������������
                 //Main.loginuser.Text = name;//Ϊ�ֶθ�ֵ
                 //Main.Logintime.Text = time;//Ϊ�ֶθ�ֵ
                 Main.Show();//��ʾ������
             }
             else
             {
                 txtpwd.Text = "";//����ı�����
                 comboBox1.Text = "";//����ı�����
                 MessageBox.Show("�û������������", "��ʾ",//������Ϣ�Ի���
                     MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
         }
     }
     catch (Exception ex)//�����쳣
     {
         MessageBox.Show(ex.Message);//������Ϣ�Ի���
     }
 }
Example #2
0
        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (nameTxt.Text == "" || pwdTxt.Text == "")//判断用户名和密码是否为空
            {
                MessageBox.Show("用户名或密码不能为空!",//弹出消息对话框
                    "提示", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;//退出事件
            }
            else
            {
                string name = nameTxt.Text.Trim();//移除用户名前部和后部的空格
                string pwd = pwdTxt.Text.Trim();//移除密码前部和后部的空格

                SqlConnection conn = DBClass.DBConnection.MyConnection();//创建数据库连接对象
                conn.Open();//连接到SQL数据库
                //SqlCommand cmd = new SqlCommand(//创建数据库命令对象
                //    "select * from HPES_user where name='" +
                //    name + "' and password='******'", conn);

                SqlCommand cmd = new SqlCommand("select * from HPES_user where name='" + name + "' and password='******'", conn);

                SqlDataReader sdr = cmd.ExecuteReader();//得到数据读取器对象

                sdr.Read();//读取一条记录

                if (sdr.HasRows)//判断是否包含数据
                {
                    string time = DateTime.Now.ToString();//得到系统时间字符串

                    string sql = "update HPES_user set logintime='" //设置更新数据库的SQL语句
                        + time + "' where name='" + name + "'";
                    operate.OperateData(sql);//更新数据库内容
                    conn.Close();//关闭数据库连接
                    //this.Hide();//隐藏窗体

                    //FrmMain main = new FrmMain();//创建主窗体对象

                    ////main.loginuser.Text = name;//为主窗体字段赋值
                    ////main.Logintime.Text = time;//为主窗体字段赋值
                    //main.Show();//显示主窗体

                    frmMain frmMain = new frmMain();
                    frmMain.Show();
                    this.Hide();

                }
                else
                {
                    nameTxt.Text = "";//清空用户名
                    pwdTxt.Text = "";//清空密码
                    MessageBox.Show("用户名或密码错误!", "提示",//弹出消息对话框
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }