Compare the code of this example with ADO doc_AdoConnected3.htm

Intro to LINQ : (LINK -sounds), Language Integrated Query

Start a new web site, and then add a style sheet

Add this code to the web config file

<connectionStrings>
<add name="adoCnn" connectionString="Data Source=MANAS6\SQLEXPRESS;Initial Catalog=adoConnect1;Integrated Security=True" providerName="System.Data.SqlClient;"/>
</connectionStrings>

Add a class and a style sheet

Attach a style sheet

Below, shows the server explorer and the database/table we will be using in this example.

 

Codes main.css

 form
{
width:500px; background-color:#FFFFCC; height:auto;
}
#div1
{
position:absolute; background-color: #FFFFCC;z-index:2;
top: 20px; left: 10px;width: 500px; height:auto;
padding-left:10px; padding-bottom:10px;
}
body  {  background-color: Gray;  }
table  {   width:450px;   }
#GridView1
{
border-color: #CC6600; border-style:solid; width:450px; border-width:6px;
}
 

Code adoMain.cs -Class, working as modulator, and

using System;
using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Summary description for adoMain.cs
/// </summary>
public class adoMain
{
public string strResult;
public static readonly string _connectionString;
private int _sid; public int newsid;
private string _job; private string _firstName; private string _lastName;
private string _note; public string strTime; public string strDelete;
static adoMain()
{
_connectionString = WebConfigurationManager.ConnectionStrings["adoCnn"].ConnectionString;

}
public int sid
{
get { return _sid; }
set { _sid = value; }
}
public string Job
{
get { return _job; }
set { _job = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string Note
{
get { return _note; }
set { _note = value; }
}

Note the code Default.aspx.cs

using System;
using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Collections;

// using Generics
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
adoMain objresult = new adoMain();
protected void Page_Load(object sender, EventArgs e)
{
var varLinq = GetAll();
this.GridView1.DataSource = varLinq;
this.GridView1.DataBind();
}
public List<adoMain> GetAll()
{
List<adoMain> results = new List<adoMain>();
SqlConnection cnn = new SqlConnection(adoMain._connectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM Main2", cnn);
cnn.StatisticsEnabled = true;
using (cnn)
{
cnn.Open();
SqlDataReader reader = cmd.ExecuteReader();

objresult.strResult += "<br/> Connected and DataReader Loader";
while (reader.Read())
{
adoMain adObj = new adoMain();
// adObj.sid = (String)reader["sid"].ToString();
adObj.sid = (int)reader["sid"];
adObj.Job = (string)reader["Job"];
adObj.FirstName = (string)reader["FirstName"];
adObj.LastName = (string)reader["LastName"];
adObj.Note = (string)reader["Note"];
results.Add(adObj);
}
}
 
objresult.strResult += "Data Loaded";
return results;
}
}

Code Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.LINQ4 : Compared ADO / LINQ </title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="div1">
<asp:GridView ID="GridView1" runat="server" Caption="Compare LINQ / ADO" >
</asp:GridView>
</div>
</form>
</body>
</html>
 

Run time view and verification with the data table.