May 11, 2012

LINQ TO SQL SAMPLE-C#


 private void Form1_Load(object sender, EventArgs e)
        {

            refResh();

        }
        //sql connection
        private void refResh()
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var src = from c in dc.AÜs
                      select new {c.Id ,c.Soyad,  c.Ad , c.Job};
            dataGridView1.DataSource = src;
       
        }
        //Update
        private void button1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var src = (from c in dc.AÜs
                      where c.Id==int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString())
                      select c).SingleOrDefault();
          
           
            if ((textBox3.Text != "")&&(textBox2.Text!=""))
            {
                src.Soyad =textBox2.Text;
                src.Ad = textBox3.Text;
               
            }
            if ((textBox2.Text!= "") && (textBox3.Text == ""))
            {
                src.Ad = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                src.Soyad =textBox2.Text;
            }
           if ((textBox3.Text!= "") && (textBox2.Text == ""))
            {
                src.Soyad = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                src.Ad=textBox3.Text;
            }
           src.Job =comboBox1.Text;
          
            dc.SubmitChanges();
            refResh();
            MessageBox.Show("Updated");

        }
        //delete
        private void button2_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var src = (from c in dc.AÜs
                       where c.Id==int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString())

                       select c).SingleOrDefault();

            if (src != null)
            {
                dc.AÜs.DeleteOnSubmit(src);


            }

            dc.SubmitChanges();
            refResh();
            MessageBox.Show("Deleted");
            textBox1.Text = "";
            textBox6.Text = "";


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        //insert
        private void button3_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            AÜ cust = new AÜ();
            cust.Ad = textBox5.Text;
            cust.Soyad = textBox4.Text;
            cust.Job = comboBox2.Text;
            dc.AÜs.InsertOnSubmit(cust);
            dc.SubmitChanges();
            refResh();
            MessageBox.Show("Inserted");
            textBox4.Text = "";
            textBox5.Text = "";
            comboBox2.Text = "";
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox6.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            comboBox1.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        }

No comments:

Post a Comment