INSERT QUERY

INSERT QUERY :

The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
library -   using System.Data.SqlClient;

Syntax:
insert into stu(table_name) values( ' " + control_1+ " ' , ' " + control_2 + " ');

1. Create default.aspx page then drag and drop textbox and button.

2. create database file database.mdf then create table.
https://aspdotnetpb.blogspot.in/2017/05/how-to-create-database-in-net.html

3. write code on button click.
   Note:
    using System.Data.SqlClient; 
     library used in SqlServer Database
   @ - DataBase Path Finder.       

4. Code:
   
Path:
    string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";

Click Here for how to get path :
 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cm;
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
        cn = new SqlConnection(cs);
        cn.Open();
        string k = "insert into stu values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
        cm = new SqlCommand(k,cn);
        cm.ExecuteNonQuery();
    }
}


Output:

Popular Posts