using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;


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

        private void connect_Click(object sender, EventArgs e)
        {
            
            ServerConnection serverConnection = new ServerConnection();
            if (!defaultConnection.Checked)
            {
                if(ApplicationName.Text != "")
                {
                    serverConnection.ApplicationName = ApplicationName.Text;
                }
                if(UserName.Text != "")
                {
                    serverConnection.ConnectAsUserName = UserName.Text;
                }
                if (UserPassword.Text != "")
                {
                    serverConnection.ConnectAsUserPassword = UserPassword.Text;
                }
                if(ServerInstance.Text != "")
                {
                    serverConnection.ServerInstance = ServerInstance.Text;
                }
            }
            serverConnection.LoginSecure = windowsAuthentication.Checked;
            if (!serverConnection.LoginSecure)
            {
                if (UserName.Text != "")
                {
                    serverConnection.Login = UserName.Text;
                }
                if (UserPassword.Text != "")
                {
                    serverConnection.Password = UserPassword.Text;
                }
            }
            serverConnection.DatabaseName = "Scratch";
            serverConnection.ConnectAsUserName = UserName.Text;
            if (UserName.Text != "")
            {
                serverConnection.ConnectAsUser = true;
            }
            if (UserPassword.Text != "")
            {
                serverConnection.ConnectAsUserPassword = UserPassword.Text;
            }
            try{
                serverConnection.Connect();
            }
            catch
            {
            }
            IsOpen.Checked = serverConnection.IsOpen;
            if (serverConnection.IsOpen)
            {
                textBox2.Text = serverConnection.TrueLogin;
                textBox1.Text = serverConnection.SqlConnectionObject.ConnectionString;
                PID.Text = serverConnection.ProcessID.ToString();
            }
            
        }

  
 
    }
}