using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace WindowsFORMCONNECTION
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//create object of Connection Class..................
SqlConnection con = new SqlConnection();
//Set Connection String property of Connection object..................
con.ConnectionString = @"Data Source=KING_OF_JAUNPUR\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
//Open Connection..................
con.Open();
//Create object of Command Class................
SqlCommand cmd = new SqlCommand();
//set Connection Property of Command object.............
cmd.Connection = con;
//Set Command type of command object
//1.StoredProcedure
//2.TableDirect
//3.Text (By Default)
cmd.CommandType = CommandType.Text;
//Set Command text Property of command object.........
cmd.CommandText = "insert into Customer(SrNo,FirstName,LastName,Dob) values(" + Convert.ToInt32(textBox1.Text) + ",'" +textBox2.Text+ "','" + textBox3.Text + "','" +Convert.ToDateTime(textBox4.Text).ToShortDateString() + "')";
//Execute command by calling following method................
// 1.ExecuteNonQuery()
// It query using for insert,delete,update command...........
//2.ExecuteScalar()
// It query return a single value and insert all record...................(using select,insert command)
// 3.ExecuteReader()
// It query return one or more than one record....................................
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved");
//TextBoxClear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Customer where SrNo='" + Convert.ToInt32(textBox1.Text) + "'";
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox2.Text = dr["FirstName"].ToString();
textBox3.Text = dr[2].ToString();
textBox4.Text = dr.GetDateTime(3).ToShortDateString();
}
else
MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Delete from Customer where SrNo='" + Convert.ToInt32(textBox1.Text) + "'";
cmd.Connection = con;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Data delete Successfully for SrNo" + textBox1.Text);
// TextBoxClear();
}
else
{
MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
con.Close();
}
private void button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123;";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update Customer set FirstName='" + textBox2.Text + "',LastName='" + textBox3.Text + "',Dob='" +Convert.ToDateTime(textBox4.Text ) + "' where SrNo='"+Convert.ToInt32(textBox1.Text )+"'";
cmd.Connection = con;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Data updated Successfully for SrNo" +textBox1.Text );
//TextBoxClear();
}
else
{
MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
con.Close();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace WindowsFORMCONNECTION
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//create object of Connection Class..................
SqlConnection con = new SqlConnection();
//Set Connection String property of Connection object..................
con.ConnectionString = @"Data Source=KING_OF_JAUNPUR\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
//Open Connection..................
con.Open();
//Create object of Command Class................
SqlCommand cmd = new SqlCommand();
//set Connection Property of Command object.............
cmd.Connection = con;
//Set Command type of command object
//1.StoredProcedure
//2.TableDirect
//3.Text (By Default)
cmd.CommandType = CommandType.Text;
//Set Command text Property of command object.........
cmd.CommandText = "insert into Customer(SrNo,FirstName,LastName,Dob) values(" + Convert.ToInt32(textBox1.Text) + ",'" +textBox2.Text+ "','" + textBox3.Text + "','" +Convert.ToDateTime(textBox4.Text).ToShortDateString() + "')";
//Execute command by calling following method................
// 1.ExecuteNonQuery()
// It query using for insert,delete,update command...........
//2.ExecuteScalar()
// It query return a single value and insert all record...................(using select,insert command)
// 3.ExecuteReader()
// It query return one or more than one record....................................
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved");
//TextBoxClear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Customer where SrNo='" + Convert.ToInt32(textBox1.Text) + "'";
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox2.Text = dr["FirstName"].ToString();
textBox3.Text = dr[2].ToString();
textBox4.Text = dr.GetDateTime(3).ToShortDateString();
}
else
MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Delete from Customer where SrNo='" + Convert.ToInt32(textBox1.Text) + "'";
cmd.Connection = con;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Data delete Successfully for SrNo" + textBox1.Text);
// TextBoxClear();
}
else
{
MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
con.Close();
}
private void button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123;";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update Customer set FirstName='" + textBox2.Text + "',LastName='" + textBox3.Text + "',Dob='" +Convert.ToDateTime(textBox4.Text ) + "' where SrNo='"+Convert.ToInt32(textBox1.Text )+"'";
cmd.Connection = con;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Data updated Successfully for SrNo" +textBox1.Text );
//TextBoxClear();
}
else
{
MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
con.Close();
}
}
}