首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

winfrom窗体跳转有关问题

2013-07-09 
winfrom窗体跳转问题我的意思是用窗体设计了一个登录界面,登陆的账号密码我想是通过数据库验证账号密码是

winfrom窗体跳转问题
我的意思是用窗体设计了一个登录界面,登陆的账号密码我想是通过数据库验证账号密码是否正确。点击登录就跳转到主窗体。这个怎么弄? 界面
[解决办法]
在登陆窗口中添加一个按钮,在这个按钮中添加下面的代码

//登入
            int count = 0;
            string connStr = "连接字符串,在你的程序中连接数据库时直接复制过来";
            //1、写连接字符串,打开数据库的链接
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();

            //2、发送sql语句命令,并执行
            SqlCommand com = new SqlCommand();
            //com.CommandText = "select username,password from users where username='" + this.textBox1.Text + "'";
            com.CommandText = "select * from student";
            com.CommandType = CommandType.Text;
            com.Connection = conn;

            SqlDataReader dr = com.ExecuteReader();

            //3、查询出来的数据进行遍历

            while (dr.Read())
            {

                string username = dr.GetString(0).Replace(" ", "");
                string password = dr.GetString(1).Replace(" ", "");
                if (username.Equals(this.IDtextBox.Text) && password.Equals(this.PasswortextBox.Text))


                {
                    Count = 1;
                }

            }

            //4、关闭数据库
            dr.Close();
            conn.Close();

            if (Count == 1)
            {
                MessageBox.Show("登陆成功");
                MainForm main = new MainForm();
                main.Show();
            }
            else
            {
                this.textBox1.Text = "";
                this.textBox2.Text = "";
                MessageBox.Show("用户不存在,或者密码错误。请重新输入!");
            }
            MainForm main = new MainForm();
            main.Show();
            this.Hide();
[解决办法]


namespace mySchool
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnYes_Click(object sender, EventArgs e)


        {
            string userName = txtName.Text;
            string passwd = txtPwd.Text;
            string connectionString = "Data Source=orcl;User Id=mySchool;Password=my;";

            OracleConnection conn = new OracleConnection(connectionString);
            try {
                //连接数据库
                conn.Open();
                string sql = String.Format("select count(*) from users where username=\'{0}\' and " +
                                            "password=\'{1}\'", userName, passwd);
                //创建OracleCommand对象
                OracleCommand cmd = new OracleCommand(sql, conn);
                //CommandType设置为执行SQL语句
                cmd.CommandType = CommandType.Text;
                //执行CommandType.Text指定的操作
                int count = Convert.ToInt32(cmd.ExecuteScalar());
                //获取执行结果
                if (count == 1) {
                    MessageBox.Show("欢迎进入成绩管理系统!", "登陆成功", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);


                    this.Visible = false;//隐藏登陆窗口
                    MainForm mainForm = new MainForm(conn);//创建主窗体
                    mainForm.Visible = true;//设主窗口可见
                }
                else {
                    MessageBox.Show("用户名或密码错误!", "登陆失败", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPwd.Text = "";
            txtName.Focus();
        }
    }
}

热点排行