如何实现登陆窗体,即输入用户名和密码后才能进入应用程序?用户,密码得从数据库获得。 using System.Data.SqlClient; namespace _图书馆管理系统_ 

    public partial class Frm_User : Form 
    { 
        public Frm_User() 
        { 
            InitializeComponent(); 
        }         private void button1_Click_1(object sender, EventArgs e)//确定按钮 
        { 
            SqlConnection con = new SqlConnection("server=.;database=Library;Integrated Security=USERB"); 
            con.Open(); 
            //select * from USERB where name='ganqiyin' and password='123456' 
            SqlCommand cmd = new SqlCommand("select * from  USERB where name='" + textBox1.Text + "'and password='" + textBox2.Text + "'"); 
            cmd.ExecuteScalar(); 
            if (); 
            { 
                Frm_Main Main = new Frm_Main(); 
                Main.Show(); 
                this.Hide(); 
            } 
        }       我就会一点,请大家帮我下,谢谢!

解决方案 »

  1.   

    select password from USERB where name='ganqiyin';string password=string.Empty;//根据用户名称从数据库中查到的密码if(password==textBox2.Text&&password!="")
    {}
      

  2.   

    你的代码错了,
    private void button1_Click_1(object sender, EventArgs e)//确定按钮 
            { 
                SqlConnection con = new SqlConnection("server=.;database=Library;Integrated Security=USERB"); 
                con.Open(); 
                //select * from USERB where name='ganqiyin' and password='123456' 
                SqlCommand cmd = new SqlCommand("select * from  USERB where name='" + textBox1.Text + "'and password='" + textBox2.Text + "'",conn); 
                [color=#FF0000SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);[/color]  
                if (reader.HasRows)          
                {  
                    Frm_Main Main = new Frm_Main(); 
                    Main.Show(); 
                    this.Hide(); 
                } 
            } 
      

  3.   

    private void button1_Click_1(object sender, EventArgs e)//确定按钮 
            { 
                SqlConnection con = new SqlConnection("server=.;database=Library;Integrated Security=USERB"); 
                con.Open(); 
                //select * from USERB where name='ganqiyin' and password='123456' 
                SqlCommand cmd = new SqlCommand("select * from  USERB where name='" + textBox1.Text + "'and password='" +    textBox2.Text + "'",conn); 
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);  
                if (reader.HasRows)    //判断是否能够读取到数据      
                {  
                    Frm_Main Main = new Frm_Main(); 
                    Main.Show(); 
                    this.Hide(); 
                } 
            } 
      

  4.   

    cmd.ExecuteScalar(); 返回的是第一行第一列的值,所以可以判断如果返回的有值只为正确的
    假如数据库里为string,则可写成
    string name=cmd.ExecuteScalar(); 
    if(!string.IsNullOrEmpty(name)||name!=null)
    {
       执行你的操作
    }
      

  5.   

    和3楼的思维差不多.不过3楼那的"conn"是"con",你的代码都错了,自己对着书是不是打错了..
    private void button1_Click_1(object sender, EventArgs e)//确定按钮 
            { 
                SqlConnection con = new SqlConnection("server=.;database=Library;Integrated Security=USERB"); 
                con.Open(); 
                //select * from USERB where name='ganqiyin' and password='123456' 
                SqlCommand cmd = new SqlCommand("select * from  USERB where name='" + textBox1.Text + "'and password='" +    textBox2.Text + "'",con); 
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);  
                if (reader.HasRows)    //判断是否能够读取到数据      
                {  
                    Frm_Main Main = new Frm_Main(); 
                    Main.Show(); 
                    this.Hide(); 
                } 
            }