当前位置:编程学习 > C#/ASP.NET >>

c#登陆.CS

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Data.Sql;
namespace MyProject
{
    public partial class 主界面 : Form
    {
     
       

        
        public 主界面()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            bool isValidUser = false;   // 标识是否为合法用户            
            string message = "";

            if (ValidateInput())  // 验证输入成功,显示窗体
            {
                // 验证用户是否为合法用户
                isValidUser = ValidateUser(
                    textBox1.Text,
                    textBox2.Text,
                    comboBox1.Text,
                    ref message
                    );

                // 如果是合法用户,就转到相应的窗体
                if (isValidUser)
                {
                    // 记录登录用户名和登录类型
                    UserHelper.loginId = textBox1.Text;
                    UserHelper.loginType = comboBox1.Text;


                    switch (comboBox1.Text)
                    {
                        case "单位用户":
                            单位用户主界面 unit = new 单位用户主界面();
                            unit.Show();
                            this.Hide();
                            break;

                        case "管理员":
                            管理员主界面 admin = new 管理员主界面();
                            admin.Show();
                            this.Hide();
                            break;
                        case "专家用户":
                            专家用户主界面 expert = new 专家用户主界面(); ;
                            expert.Show();
                            this.Hide();
                            break;


                    }

                }
                else  // 验证合法用户失败,给出提示
                {
                    MessageBox.Show(message, "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private bool ValidateInput()
        {
            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
                return false;
            }
            else if (textBox2.Text.Trim() == "")
            {
                MessageBox.Show("请输入密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox2.Focus();
                return false;
            }
            else if (comboBox1.Text.Trim() == "")
            {
                MessageBox.Show("请选择登录类型", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comboBox1.Focus();
                return false;
            }
            else
            {
                return true;
            }
        } 
            
       


      
      
       


    
 
        // 验证用户是否合法
        // 传入用户名、密码、登录类型
        // 合法返回 True,不合法返回 False
        // message 参数用来记录验证失败的原因
        private bool ValidateUser(string loginId, string loginPwd, string loginType, ref string message)
        {
            if (loginType == "管理员")
            {
                // 查询字符串
                string sql = string.Format("SELECT COUNT(*) FROM managerInformation where loginId ='{0}' AND loginPwd='{1}'",loginId, loginPwd);

                // 测试数据库连接
                try
                {
                   // 创建 Command 对象
                  
                    SqlCommand command = new SqlCommand(sql, DBHelper.Connection);

                    // 打开数据库连接
                    DBHelper.Connection.Open();
                    
                    // 验证是否为合法用户
                    int count = (int)command.ExecuteScalar();
                    if (count < 1)
                    {
                        message = "用户名或密码不存在!";
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
               catch (Exception ex)
                {
                    message = "操作数据库出错!";
                    Console.WriteLine(ex.Message);
                    return false;
                }
                finally
                {
                    // 关闭数据库连接
                    DBHelper.Connection.Close();
                   
                }
            }else 
                 if (loginType == "单位用户")
            {
                // 查询字符串
                string sql = string.Format("SELECT * FROM companyInformation where loginId ='{0}' AND loginPwd='{1}'",  loginId, loginPwd);
                DataTable dt = DBHelper.ExecuteDataTable(sql);
              

                UserHelper.name = (string)dt.Rows[0]["companyName"];
                // 测试数据库连接
               try
                {
                    // 创建 Command 对象
                    SqlCommand command = new SqlCommand(sql, DBHelper.Connection);

                    // 打开数据库连接
                    DBHelper.Connection.Open();

                    // 验证是否为合法用户
                    int count = (int)command.ExecuteScalar();
                    if (count < 1)
                    {
                        message = "用户名或密码不存在!";
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    message = "操作数据库出错!";
                    Console.WriteLine(ex.Message);
                    return false;
                }
                finally
                {
                    // 关闭数据库连接
                    DBHelper.Connection.Close();
                }
            }
                 else if (loginType == "专家用户")
            {
                // 查询字符串
                string sql = string.Format("SELECT COUNT(*) FROM expertInformation where loginId ='{0}' AND loginPwd='{1}'",
                    loginId, loginPwd);

                // 测试数据库连接
                try
                {
                    // 创建 Command 对象
                    SqlCommand command = new SqlCommand(sql, DBHelper.Connection);

                    // 打开数据库连接
                    DBHelper.Connection.Open();

                    // 验证是否为合法用户
                    int count = (int)command.ExecuteScalar();
                    if (count < 1)
                    {
                        message = "用户名或密码不存在!";
                        return false;
                    }
                    else
                    {
                        UserHelper.loginId = loginId;
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    message = "操作数据库出错!";
                    Console.WriteLine(ex.Message);
                    return false;
                }
                finally
                {
                    // 关闭数据库连接
                    DBHelper.Connection.Close();
                }
                
            }
            message = "请选择类型登录";
            return false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

      
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


代码如下。 提示错误错误 1 无法将文件“obj\x86\Debug\MyProject.exe”复制到“bin\Debug\MyProject.exe”。文件“bin\Debug\MyProject.exe”正由另一进程使用,因此该进程无法访问此文件。 MyProject
求帮助 --------------------编程问答-------------------- 因为你允许了2次吧,所以在进程里面已经有一个MyProject了,你在运行就会出现你的这个错误。
解决办法,停止允许,或者直接吧进程里面的MyProject进程关了. --------------------编程问答-------------------- 看下是不是已经有程序实例在跑了 --------------------编程问答--------------------
引用 1 楼 gyc1105 的回复:
因为你允许了2次吧,所以在进程里面已经有一个MyProject了,你在运行就会出现你的这个错误。
解决办法,停止允许,或者直接吧进程里面的MyProject进程关了.

不是允许 是运行 ,写错了 --------------------编程问答--------------------
引用楼主 hfdpgd 的回复:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ……



那为什么我运行程序的时候总是先弹出 操作数据库的界面,然后再点击登录又可以连接数据库了 呢?
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,