SELECT Query

SELECT Query:

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the form of a result table. These result tables are called result-sets. Retrieval of database with the help of Select Query. 

library -   using System.Data.SqlClient;

Syntax:
Select * from student(table) where rn = ' " + TextBox1.Text+ " ';

1. Design

2. Write code on Button_Click3


Code:
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 Button4_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 = "select * from stu where rn = '"+TextBox1.Text+"'";
        cm = new SqlCommand(k,cn);
        dr = cm.ExecuteReader();
        if (dr.Read())
        {
            TextBox2.Text = dr[1].ToString();
            TextBox3.Text = dr[2].ToString();
        }
    }

}

3. Output
Enter Roll No. in text box then click on Show Button.


Popular Posts