c# winform连接sql server实现跳转登录页面简单实例

时间:2024-03-31 17:39:34

一个简单的登录页面

private void NewMethod()
        {
            //获取文本框中的值
            username = this.textBox1.Text.Trim();
            userpassword = this.textBox2.Text.Trim();

            if (username.Equals("") || userpassword.Equals(""))//用户名或密码为空
            {
                MessageBox.Show("用户名或密码不能为空");
                return;
            }
            else//用户名或密码不为空
            {   
                                //到数据库中验证
                SqlConnection mycon = new SqlConnection(@"Server=服务器名称;Database=数据库名;user id=用户名;pwd=密码");

                mycon.Open();         //建立连接 
             
                SqlCommand com = new SqlCommand(); 
                com.Connection = mycon;
                com.CommandType = CommandType.Text;
                com.CommandText = "select * from [表名] where username='" + this.textBox1.Text + "'and userpassword='" + this.textBox2.Text + "'";
                SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);//执行SQL语句
                //CommandBehavior.CloseConnection 断开链接
                
                if (dr.Read())         //判断
                {
                    username  = textBox1.Text;
                    userpassword = textBox2.Text;
                    //一旦连接成功了就弹出窗口
                    MessageBox.Show("登录成功!", "登录提示", MessageBoxButtons.OK);

                }
                else
                {
                    //信息错误,判断条件不成立
                    MessageBox.Show("密码错误", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                mycon.Close();     //关闭链接

 

 

            }
            Form f2 = new FormMain(); //跳转

            this.Hide();

            f2.ShowDialog();

            this.Dispose();

        }

 

 

c# winform连接sql server实现跳转登录页面简单实例

 

c# winform连接sql server实现跳转登录页面简单实例

 

c# winform连接sql server实现跳转登录页面简单实例

 

 

c# winform连接sql server实现跳转登录页面简单实例

c# winform连接sql server实现跳转登录页面简单实例

 

 

c# winform连接sql server实现跳转登录页面简单实例