Tuesday, May 11, 2010

Database Connectivity

The following example shows how to connect the C# .net form with the SQL Server 2005




The Source code is given below............................

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;
using Microsoft.VisualBasic;


namespace DBConnect
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlDataAdapter da;
        SqlCommandBuilder cb;
        DataSet ds;
        int rno = 0;
        public Form1()
        {
            InitializeComponent();
        }
      
        private void ShowData()
        {
            textBox1.Text = ds.Tables[0].Rows[rno][0].ToString();
            textBox2.Text = ds.Tables[0].Rows[rno][1].ToString();
            textBox3.Text = ds.Tables[0].Rows[rno][2].ToString();
            textBox4.Text = ds.Tables[0].Rows[rno][3].ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("user ID=sa; Password=sasa; Database=employee123");
          
            da = new SqlDataAdapter("Select * From concern", con);
            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            ds = new DataSet();
            da.Fill(ds, "concern");
            ShowData();

        }
        //First
        private void button3_Click_1(object sender, EventArgs e)
        {
            rno = 0;
            ShowData();
        }
        //insert
        private void button6_Click_1(object sender, EventArgs e)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr[0] = textBox1.Text;
            dr[1] = textBox2.Text;
            dr[2] = textBox3.Text;
            dr[3] = textBox4.Text;
            ds.Tables[0].Rows.Add(dr);
            MessageBox.Show("Inserted");
            button3.PerformClick();

        }
        //update
        private void button7_Click_1(object sender, EventArgs e)
        {
            ds.Tables[0].Rows[rno][0] = textBox1.Text;
            ds.Tables[0].Rows[rno][1] = textBox2.Text;
            ds.Tables[0].Rows[rno][2] = textBox3.Text;
            ds.Tables[0].Rows[rno][3] = textBox4.Text;
            MessageBox.Show("Updated");

        }
        //delete
        private void button8_Click_1(object sender, EventArgs e)
        {
            ds.Tables[0].Rows[rno].Delete();
            MessageBox.Show("Deleted");
            button3.PerformClick();

        }
        //Save
        private void button9_Click_1(object sender, EventArgs e)
        {
            cb = new SqlCommandBuilder(da);
            da.Update(ds, "concern");
            MessageBox.Show("Data Saved to DB");
        }
        //Find
        private void button10_Click_1(object sender, EventArgs e)
        {
           /* int sno = int.Parse(Interaction  .InputBox("Enter Employee No", "Question Message", "", 100, 100));
            DataRow dr = ds.Tables[0].Rows.Find(sno);
            textBox1.Text = dr[0].ToString();
            textBox2.Text = dr[1].ToString();
            textBox3.Text = dr[2].ToString();
            textBox4.Text = dr[3].ToString();*/

        }

      
        //next
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (rno < ds.Tables[0].Rows.Count - 1)
            {
                rno++;
                ShowData();
            }
            else
                MessageBox.Show("Last Record");
        }
        //Previous
        private void button2_Click_1(object sender, EventArgs e)
        {
            if (rno > 0)
            {
                rno -= 1;
                ShowData();
            }
            else
                MessageBox.Show("First Record");
        }
        //last
        private void button4_Click_1(object sender, EventArgs e)
        {
            rno = ds.Tables[0].Rows.Count - 1;
            ShowData();
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }
    }
}

1 comment: