Saturday, 23 July 2011

Another C# Sharp WinForm With Massage Box Creating Connection To SqlServer adding,delete,update Data Through Textbox


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=WEBNET;database=myfirstdb;user=sa;password=aptech");
            string insert;
            insert = "insert into employee values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "' )";
            SqlCommand cm = new SqlCommand(insert,cn);
            cn.Open();
            cm.ExecuteNonQuery();
            MessageBox.Show("Record added succesfully");
            cn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=WEBNET;database=myfirstdb;user=sa;password=aptech");
            string delete;
            delete = "delete from employee where empno='" + textBox1.Text + "' ";
            SqlCommand cm = new SqlCommand(delete, cn);
            cn.Open();
            cm.ExecuteNonQuery();
            cn.Close();


 private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=LAB2-80;database=myfirstdb;user=sa;password=aptech");
            string update;
            update = "update emp set empno='"+textBox1.Text+"',name='"+textBox2.Text+"',location='"+textBox3.Text+"' where empno='"+textBox1.Text+"' ";
            SqlCommand cm = new SqlCommand(update, cn);
            cn.Open();
            cm.ExecuteNonQuery();
            MessageBox.Show("Record updated succesfully");
            cn.Close();
        }



        }
    }
}

No comments:

Post a Comment