Sql Connection with select command in asp.net
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
// Using System.Data.OralceClient;
public partial class _Default : System.Web.UI.Page
{ protected void Page_Load(objectsender, EventArgs e)
{
// First Trick
SqlConnection con = newSqlConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True");
con.Open();
SqlCommand cmd = newSqlCommand("Select *from ProductTable", con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
// Second Trick Connection Open and Close Using Statement
using (SqlConnectioncon = new SqlConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True"))
{
SqlCommand cmd = newSqlCommand("Select *from ProductTable", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
// 2 Code For Oracle Database
/*
OracleConnection con = new OracleConnection(@"Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True");
con.Open();
OracleCommand cmd = new OracleCommand("Select *from ProductTable", con);
OracleDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
*/
}
}
Comments
Post a Comment