Wednesday, 29 February 2012

Connectivity with Sql Server with ADO .Net


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 MyDataConnectivity
{
  
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand cmd;
       
        public Form1()
        {
            InitializeComponent();
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {
           
          
        }

        private void btn_insert_Click(object sender, EventArgs e)
        {


// Connectivity in  ADO .NET ........with Sql Parameter

try
  {
   con = new SqlConnection();
con.ConnectionString = "Data Source=KUSH-PC\\SQLSERVER2008; Initial Catalog=MyDemo; Integrated Security= SSPI";
con.Open();
cmd = new SqlCommand();
cmd.CommandText = "insert into customer_details (Customer_id,Customer_Fname,Customer_Lname,DOB,Father_Name,Mobile_No) Values('" + txtcustomerid.Text + "','" + txtcusfname.Text + "','" + txtcuslname.Text + "','" + txtcusdob.Text + "','" + txtfathername.Text + "','" + txtmobileno.Text + "')";
 cmd.Connection = con;
 cmd.ExecuteNonQuery();
 MessageBox.Show("SAVED");
                con.Close();
   }
     catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

        
// Connectivity in  ADO .NET ....................................with Sql Parameter

            try
            {
con = new SqlConnection("Data Source=KUSH-PC\\SQLSERVER2008; Initial Catalog=MyDemo; Integrated Security= SSPI");
 con.Open();
cmd = new SqlCommand("insert into customer_detailsvalues(@Customer_id,@Customer_Fname,@Customer_Lname,@DOB,@Father_Name,@Mobile_No)", con);
       cmd.Parameters.AddWithValue("@customer_id", txtcustomerid.Text);
       cmd.Parameters.AddWithValue("@customer_fname", txtcusfname.Text);
       cmd.Parameters.AddWithValue("@customer_lname", txtcuslname.Text);
       cmd.Parameters.AddWithValue("@dob", Convert.ToDateTime(txtcusdob.Text));
       cmd.Parameters.AddWithValue("@father_name", txtfathername.Text);
       cmd.Parameters.AddWithValue("@mobile_no", txtmobileno.Text);
       cmd.ExecuteNonQuery();
       MessageBox.Show("SAVED");
       con.Close();
        }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }



//Connectivity in  ADO .NET ....................................with DataSet Table Adapter (Disconnected Mode)


try
{

 DataSet1TableAdapters.customer_detailsTableAdapter da = new DataSet1TableAdapters.customer_detailsTableAdapter();
da.Insert(txtcustomerid.Text, txtcusfname.Text, txtcuslname.Text, Convert.ToDateTime(txtcusdob.Text), txtfathername.Text, txtmobileno.Text);
MessageBox.Show("SAVED");
}
 catch (Exception ex)
  {
 MessageBox.Show(ex.Message);

  }
        




}
    


}


}


0 comments:

Post a Comment