Insert, Update and Delete on Database Table through Windows Form without Using Stored Procedures

So far we have used stored procedure to insert, update and delete of any record in database. But in this article we will be using simple approach to accomplish our task. We will be writing most of our codes in C#.

So first thing is we need a table, I am using table named as " blog_tab_test2" which has 4 fields as

Empid, Name, Age and Location.

Code is as follows:

CREATE TABLE [dbo].[blog_tab_test2]

(

    [empid] [varchar](50) primary key,

    [name] [varchar](max) NOT NULL,

    [age] [numeric](18, 0) NULL,

    [location] [varchar](50) NULL,

)

In the above table empid field is primary key.

Secondly, we would have to directly code everything in C#.

C # code is 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.IO;

using System.Data.SqlClient;

 

namespace blog

{

public partial class Form3 : Form

{

SqlConnection con;

SqlCommand cmd;

 

public string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=blog;Data Source=MY-PC\SQLEXPRESS";

public Form3()

{

InitializeComponent();

}

 

 

 

//***********************insert***************************************

private void button1_Click(object sender, EventArgs e)

{

try

{

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

//*************string- sql insert statment**************************

string str1 = "Insert into blog_tab_test2(empid ,name,age,location) Values ('" + textBox1.Text + "','" + textBox2.Text + "'," + textBox3.Text + ",'" + textBox4.Text + "' )";

cmd = new SqlCommand(str1, con);

cmd.ExecuteNonQuery();

MessageBox.Show("Record Inserted successfully.");

con.Close();

}

catch (Exception ex)

{

label5.Text = ex.ToString();

}

}

 

//********************Update*********************

private void button2_Click(object sender, EventArgs e)

{

try

{

 

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

//**************string- sql update statment**************************

string str1 = "update blog_tab_test2 set empid='" + textBox1.Text + "', name='" + textBox2.Text + "',age=" + textBox3.Text + ",location= '" + textBox4.Text + "'where empid=" + textBox1.Text + "";

cmd = new SqlCommand(str1, con);

cmd.ExecuteNonQuery();

MessageBox.Show("Record updated successfully.");

con.Close();

}

catch (Exception ex)

{

 

label5.Text = ex.ToString();

 

}

}

 

//*******************************delete*************************************

private void button3_Click(object sender, EventArgs e)

{

try

{

 

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

//*********************string- sql delete statment**************************

string str1 = "delete from blog_tab_test2 where empid=" + textBox1.Text + "";

cmd = new SqlCommand(str1, con);

cmd.ExecuteNonQuery();

MessageBox.Show("Record Deleted successfully.");

con.Close();

}

catch (Exception ex)

{

 

label5.Text = ex.ToString();

}

}

}

}

Tags: , , , ,

Windows Form