Insert, Update, Delete and Searching records of database from C# application

Here we will look at how manipulation to database records is made using C# programming. Before you start coding do not forget to make connection string because this string allows C# program to connect to Sql Server. Also you have to add a namespace System.Data.SqlClient in your application which contains functionality of different classes used in connectivity.
Different Classes in System.Data.SqlClient:
There are many classes in this namespace but commonly used are:
SqlConnection
SqlCommand
SqlDataReader
SqlException
SqlDataAdapter  
Example:
Here in this application we will insert, update, delete and search records from person table placed in database CIS. Suppose we have following columns in person table. Stored procedures are used in this application you can use simple Sql statements for inset, update, delete and search.
·         Per_No
·         Per_Name
·         Per_Address
·         Per_Phone
And here is Form for Person class:
Now let’s look at coding which will insert, update, delete and search records from person table.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CISPROJECT
{
    public partial class Person : Form
    {
      
SqlConnection Mycon= new SqlConnection("Data Source=MINHAS-A65DA31B\\SQLEXPRESS;Initial Catalog=CIS;Integrated Security=True");

          SqlCommand Mycmd;
          SqlDataReader Myreader;
        public Person()
        {
            InitializeComponent();
        }

        private void Insertbutton_Click(object sender, EventArgs e)
        {
            try
            {
                   
                if (Per_NotextBox.Text != "" && Per_NametextBox.Text != "" && Per_AddtextBox.Text != "" && Per_PhonetextBox.Text != "")
                {
                   Mycmd = Mycon.CreateCommand();
                             Mycon.Open();

                    Mycmd.CommandText = "exec Insertperson'" + Per_NotextBox.Text + "','" + Per_NametextBox.Text + " ','" + Per_AddtextBox.Text + " ','" + Per_PhonetextBox.Text + "'";
                    Mycmd.ExecuteNonQuery();
                    Mycon.Close();
                    MessageBox.Show("Data Inserted");
                }
                else
                {
                    MessageBox.Show("Please provide values for all fields");
                    Per_NotextBox.Focus();
                }
            }
            catch (SqlException e1)
            {
                MessageBox.Show(e1.Message);
            }

        }

        private void Updatebutton_Click(object sender, EventArgs e)
        {
            string name, address, phone;
            if (Per_NotextBox.Text != "")
            {
                try
                {
                    Mycmd = Mycon.CreateCommand();
                             Mycon.Open();

                    Mycmd.CommandText = "select * from person where per_No='" + Per_NotextBox.Text + "'";
                    Myreader = Mycmd.ExecuteReader();
                    if Myreader.Read())
                    {
                        name = Myreader[1].ToString();
                        address = Myreader[2].ToString();
                        phone = Myreader[3].ToString();

                        Myreader.Close();


                        if (Per_NametextBox.Text != "")
                            name = Per_NametextBox.Text;
                        if (Per_AddtextBox.Text != "")
                            address = Per_AddtextBox.Text.ToString();
                        if (Per_PhonetextBox.Text != "")
                            phone = Per_PhonetextBox.Text.ToString();

                        Mycmd.CommandText = "exec  updateperson '" + name + "','" + address + "','" + phone + "','" + Per_NotextBox.Text + "'";
                        Mycmd.ExecuteNonQuery();

                        Mycon.Close();
                        MessageBox.Show("Record updated");
                    }
                    else
                    {
                        MessageBox.Show("Value do not exist in the database");
                        Per_NotextBox.Focus();
                    }

                }

                catch (SqlException ce)
                {
                    MessageBox.Show(ce.Message);
                }



            }
            else
            {
                MessageBox.Show("Please Enter a value to update the record");
                Per_NotextBox.Focus();
            }

        }

        private void Deletebutton_Click(object sender, EventArgs e)
        {
            try
            {
                if (Per_NotextBox.Text != "")
                {
                    DialogResult dlg = new DialogResult();
                    dlg = MessageBox.Show("Do you really want to delete the record", "Confirm Deletion", MessageBoxButtons.YesNo);
                    if (dlg == DialogResult.Yes)
                   
          Mycmd = Mycon.CreateCommand();
          Mycon.Open();
Mycmd.CommandText = "exec  Deleteperson '" + Per_NotextBox.Text + "'";
          Mycmd.ExecuteNonQuery();
          Mycon.Close();
          MessageBox.Show("Data Deleted");
                    }
                   
                   
                }
                else
                {
                    MessageBox.Show("Please provide Per_No to delete record");
                    Per_NotextBox.Focus();
                }
            }

            catch (SqlException e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        private void Searchbutton_Click(object sender, EventArgs e)
        {
            Mycon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from person where per_no='" + Per_NotextBox.Text + "'", Mycon);
            DataTable dt = new DataTable();
            da.Fill(dt);

            dataGridView1.DataSource = dt;


        }


     
    }

    }

Comments

  1. sql insert , Update , delete using c#
    with code , easy explanation and demo app
    http://geeksprogrammings.blogspot.com/2014/05/sql-insert-update-delete-using-c.html

    ReplyDelete

Post a Comment

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Using Progress Bar In C#

Jquery serer side datatables in asp.net