Insert Data In SQL Server From The Front End Using Windows Application

Here are the steps:

Step 1: Go to Visual studio, select file, then New project as shown below:

New project

Select Windows Forms Application from the templates and change the application name, then set the location path and click OK button.

Step 2: After clicking OK button, Solution Explorer will appear in Visual Studio as

Solution Explorer

Step 3: Go to Form1.cs and design the form. I am designing for Student Details as in the following screenshot:


Step 4: Click on Insert button and after that we will redirect to Form1.cs,  then write the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.SqlClient;  
  11. namespace Windows  
  12. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  21.         private void btnInsert_Click(object sender, EventArgs e)  
  22.         {  
  23.             SqlConnection con = new SqlConnection("Integrated security=true;Initial Catalog=tblStudent;Data source=.");  
  24.             SqlCommand cmd;  
  25.             con.Open();  
  26.             string s="insert into Student values(@p1,@p2,@p3)";  
  27.             cmd=new SqlCommand(s,con);  
  28.             cmd.Parameters.AddWithValue("@p1",txtSId.Text);  
  29.             cmd.Parameters.AddWithValue("@p2",txtSName.Text);  
  30.             cmd.Parameters.AddWithValue("@p3",txtSCourse.Text);  
  31.             cmd.CommandType = CommandType.Text;  
  32.             int i=cmd.ExecuteNonQuery();  
  33.             con.Close();  
  34.             MessageBox.Show(i+ " Row(s) Inserted ");  
  35.         }  
  36.     }  
  37. }  
Step 5: Run the application and check. You will get the following output screen:


Step 6: Enter the details and click on Insert button. You can see that a new message window is visible.

Message: 1 Row(s) has inserted.

Go to your database table and you can see New data which has been inserted is appearing in table data.

This is all about how to perform insert operation on the database through Windows application.