Tuesday 16 April 2013

Connecting .net application with Sql Server


We can connect with Sql Server from .net applications either by using Oledb or SqlClient class.

Add a new form in the project and design it as follows :-


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 WindowsFormsApp
{
    public partial class Form6 : Form
    {
        SqlCommand cmd;
        SqlConnection con;
        SqlDataReader dr;
        string SqlStr;
        public Form6()
        {
            InitializeComponent();
        }

        private void Form6_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("server=localhost; database=db1;uid=cheryl;trusted_connection=true");
            cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            LoadData();
        }
       public void LoadData()
       {
           cmd.CommandText="Select sid,sname,course from stud Order By sid ";
           dr = cmd.ExecuteReader();
           ShowData();
       }
        public void ShowData()
        {
            if (!dr.IsClosed && dr.Read())
            {
                txtid.Text = dr[0].ToString();
                txtname.Text =dr[1].ToString();
                txtcourse.Text = dr[2].ToString();
            }
            else
                MessageBox.Show("No data exists");
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            ShowData();
        }

        private void btnnew_Click(object sender, EventArgs e)
        {
            txtid.Text = txtname.Text = txtcourse.Text = "";
            dr.Close(); 
            cmd.CommandText = "Select IsNull(Max(sid),1000)+1 from stud";
            txtid.Text = cmd.ExecuteScalar().ToString();
            btninsert.Enabled = true;
            txtname.Focus();
        }
        private void ExecuteDML()
        {
            DialogResult d = MessageBox.Show("Are you sure of executing the above SQL statement?\n\n" + SqlStr, "Comfirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                cmd.CommandText = SqlStr;
                int count = cmd.ExecuteNonQuery();
                if (count > 0)
                    MessageBox.Show("Statement executed successfully");
                else
                    MessageBox.Show("Statement failed execution");
            }
        }

        private void btninsert_Click(object sender, EventArgs e)
        {
            SqlStr = "Insert into Stud(sname,course) values ('" + txtname.Text + "','" + txtcourse.Text + "')";
            ExecuteDML();
            btninsert.Enabled = false;
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            SqlStr = "Update Stud set sname ='"+ txtname.Text + "', course = '" + txtcourse.Text + "' Where sid="+txtid.Text;
            dr.Close();
            ExecuteDML();

        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            SqlStr = "Delete From Stud where sid=" + txtid.Text;
            dr.Close();
            ExecuteDML();

        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            if (con.State != ConnectionState.Closed)
            {
                con.Close();

            }
            this.Close();

        }
    }
}