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

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

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                lblstrConexion.Text = "SERVER=.\\SQLEXPRESS;Initial Catalog=" + cbListaBD.Text + ";Integrated Security=True";
                txtUser.Enabled = false;
                txtPass.Enabled = false;
            }
            else
            {
                lblstrConexion.Text = "SERVER=" + txtServerName.Text.Trim() + ";Initial Catalog=" + cbListaBD.Text + ";USER ID=" + txtUser.Text + ";PWD=" + txtPass.Text + ";";
                txtUser.Enabled = true;
                txtPass.Enabled = true;
                txtUser.Text = "";
                txtPass.Text = "";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtServerName.Focus();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (txtServerName.Text.Length == 0)
            {
                MessageBox.Show("Ingrese Nombre del Servidor");
                txtServerName.Focus();
            }
            else
            {
                DataSet ds = new DataSet();
                if (checkBox1.Checked)
                {
                    lblstrConexion.Text = "SERVER=.\\SQLEXPRESS;Initial Catalog=" + cbListaBD.Text + ";Integrated Security=True";
                }
                else
                {
                    lblstrConexion.Text = "SERVER=" + txtServerName.Text.Trim() + ";Initial Catalog=" + cbListaBD.Text + ";USER ID=" + txtUser.Text + ";PWD=" + txtPass.Text + ";";
                }
                try
                {
                    this.Cursor = Cursors.WaitCursor;
                    SqlConnection con = new SqlConnection("" + lblstrConexion.Text + "");
                    con.Open();
                    SqlCommand cmd = new SqlCommand("Select name as DATABASE_NAME from sys.sysdatabases", con);
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);
                    ad.Fill(ds);
                    con.Close();

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        cbListaBD.DataSource = ds.Tables[0];

                        label4.Visible = true;
                        cbListaBD.Visible = true;
                        btnNext.Visible = true;
                    }
                    this.Cursor = Cursors.Default;
                }
                catch (SqlException x)
                {
                    this.Cursor = Cursors.Default;
                    MessageBox.Show("Error de SQL :" + x.Message);
                }
                catch (Exception y)
                {
                    this.Cursor = Cursors.Default;
                    MessageBox.Show("Error :" + y.Message);
                }
            }
        }

        private void btnNext_Click_1(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            FormSelector frm = new FormSelector();
            lblstrConexion.Text = "SERVER=" + txtServerName.Text.Trim() + ";Initial Catalog=" + cbListaBD.Text + ";USER ID=" + txtUser.Text + ";PWD=" + txtPass.Text + ";";
            frm.lblstrConexion.Text = lblstrConexion.Text;
            frm.lblServerName.Text = txtServerName.Text;
            frm.lblDataBase.Text = cbListaBD.Text;
            frm.Show();
            this.Hide();
            this.Cursor = Cursors.Default;
        }
    }
}