using System;
using System.Windows.Forms;
//------------------------------------
using System.Data;
using System.Data.SqlClient;


namespace _17_12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.MaxLength = 20;
            textBox2.MaxLength = 20;
            textBox2.UseSystemPasswordChar = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strConn = "Server=.;" + "DataBase=MyTestDB2;" +
                             "Integrated Security=True";
            
            SqlConnection sCon = new SqlConnection(strConn);
            SqlCommand sc = new SqlCommand();
            sc.Connection = sCon;
            sc.CommandType = CommandType.Text;
            sc.CommandText = "Select * From Users Where UserName=@UserName and Password=@Password";
            
            sc.Parameters.AddWithValue("@Username", textBox1.Text.Trim());
            sc.Parameters.AddWithValue("@Password", textBox2.Text.Trim());
            
            sc.Connection.Open();
            
            SqlDataReader dr = sc.ExecuteReader();
            
            if (dr.Read() == true)
            {
                MessageBox.Show("خوش آمدید");
                sc.Connection.Close();
                new Form2().Show();
            }
            else
            {
                MessageBox.Show("دوباره سعی کنید");
                sc.Connection.Close();
                textBox1.Text = string.Empty;
                textBox2.Text = string.Empty;
            }
        }
    }
}