Search

Data Connection Example

//Data Connection
using System.Data.SqlClient;
using System.Data;

namespace killercodes_in.DataConnect
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlDataAdapter adp;
        DataSet ds;

        public string _intsec = "Data Source=127.0.0.1;Initial Catalog=Authors;Integrated Security=True;Pooling=False";
        public string ExtSec = "Data Source=127.0.0.1;Initial Catalog=Authors;Integrated Security=True;Pooling=False";
        public string _tcp_sa_Sec = "Data Source=127.0.0.1;Network Library=DBMSSOCN;Initial Catalog=master;Integrated Security=False;User ID=sa;Password=iamadmin;";
        private string _cString1 = "Data Source=(local);Initial Catalog=killercode;User Id=sa;Password=iamadmin;";

        public Form1()
        {
            InitializeComponent();
            con = new SqlConnection(_cString1);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            SqlConnection oCon;
            SqlCommand oCmd;
            string strCon;
            //oCon.ConnectionString = 


            oCon = new SqlConnection();
            oCon.ConnectionString = ExtSec;
            MessageBox.Show(oCon.ConnectionString);
            MessageBox.Show(oCon.E_Error().ToString());

            oCon.E_SetString("127.0.0.1", "master", "sa", "iamadmin");
            MessageBox.Show(oCon.ConnectionString);
            MessageBox.Show(oCon.E_Error().ToString());

            oCon.E_SetString("127.0.0.1", "master");
            MessageBox.Show(oCon.ConnectionString);
            MessageBox.Show(oCon.E_Error().ToString());

        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            SqlCommand oCmd;
            string strConn;

            try
            {
                strConn = "Data Source=(local);Initial Catalog=" + txtDb.Text + ";User ID=" + txtId.Text
                + ";Password=" + txtPsw.Text + ";";
                oCmd = new SqlCommand();
                oCmd.Connection = new SqlConnection(strConn);
                oCmd.Connection.Open();
                oCmd.CommandText = txtSql.Text;
                txtRows.Text = oCmd.ExecuteNonQuery().ToString();

                MessageBox.Show("SQL Statement succeded");

                oCmd.Connection.Close();
            }
            catch (Exception ExSCon)
            {
                MessageBox.Show(ExSCon.Message);
            }


        }

        private void chkSecurity_CheckedChanged(object sender, EventArgs e)
        {

            if (chkSecurity.Checked == true)
            {
                txtId.Enabled = false;
                txtPsw.Enabled = false;

            }
            else
            {
                txtId.Enabled = true;
                txtPsw.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                con.Close();
            }
            catch (Exception)
            {

            }

            ds = new DataSet();

            adp = new SqlDataAdapter("select * from grademstr", con);
            SqlCommandBuilder cb = new SqlCommandBuilder(adp);
            adp.Fill(ds, "grademstr");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "grademstr";
        }
    }

    public static class Extended
    {
        public static bool E_Check(this SqlConnection astr)
        {
            try
            {
                astr.Open();
                astr.Close();
                return true;
            }
            catch (Exception ee)
            {
                return false;
            }
        }

        public static bool E_Check(this SqlConnection astr, string OkMsg, string NokMsg)
        {
            try
            {
                astr.Open();
                astr.Close();
                MessageBox.Show(OkMsg, "Vola", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(NokMsg, "Oops", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }

        }

        public static bool E_Error(this SqlConnection astr)
        {
            try
            {
                astr.Open();
                astr.Close();
                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "An Error occured @ " + ee.Source, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
        }

        public static void E_SetString(this SqlConnection sqc, string IP, string db_Name, string ID, string Password)
        {
            sqc.ConnectionString = "Data Source=" + IP + ";Network Library=DBMSSOCN;Initial Catalog=" + db_Name + ";Integrated Security=False;User ID=" + ID + ";Password=" + Password + ";";
        }

        public static void E_SetString(this SqlConnection sqc, string IP, string db_Name)
        {
            sqc.ConnectionString = "Data Source=" + IP + ";Initial Catalog=" + db_Name + ";Integrated Security=True;Pooling=False";
        }
    }
}