LINQ to SQL Application
By this application we retrive SQL Data in ListBox by LINQ:-
So we have to following steps:-
Step 1:- First of all we can create table in SQL SERVER and Insert some records in the table:-
Step 2:-
We open Visual Studio and add Default.aspx page. Now add following code into your application:-
Step 3:-
Right click the application in Solution Explorer and select the Add New Item option from the context menu. This open the Add New Item dialog box.
Step 4:-
In this dialog box, select the LINQ and SQL Classes template and rename the file as MyDataClasses.dbml.
Step 5:-
Click the Add button, as shown in following image.
Now your application look like as:-
Step 6:-
Drag and drop the table from Server Explor. This action adds the table to your appplication, and you can retrieve any data from the table. As shown following image.
Connection String automatically created in your web.config file as shown in below:-
Step 7:-
Now go to Defaul.aspx design page and double click on Display All button and write the following code in your project:-
Step 8:-
Now run the application and click on Display All button and get out put:-
So we have to following steps:-
Step 1:- First of all we can create table in SQL SERVER and Insert some records in the table:-
CREATE DATABASE dbSantoshTest use dbSantoshTest create table tblLinqDemo ( Id int identity(100,1),Name varchar(200),Branch varchar(100),Address varchar(200) ) --INSERT SOME RECORDS INSERT INTO tblLinqDemo VALUES('Santosh kumar singh','IT','A-23, SOUHT GANESH NAGER') INSERT INTO tblLinqDemo VALUES('Pooja','CSE','B-76, Plot No.-101,Jaipur') INSERT INTO tblLinqDemo VALUES('Manorma Arya','MCA','Patna, Bihar') INSERT INTO tblLinqDemo VALUES('Tabrez Alam','BCA','D-120,Darbhanga, Bihar') INSERT INTO tblLinqDemo VALUES('Abhina Kumar','ME','A-23,Village-Pachrukhi, P.O.-Lagma, Bihar') INSERT INTO tblLinqDemo VALUES('Pramod kumar sah','IT','Kamlawari, Madhubani')
Step 2:-
We open Visual Studio and add Default.aspx page. Now add following code into your application:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <center> <h1>LINQ TO SQL APPLICATION</h1> <asp:ListBox ID="lbDetail" runat="server" Height="300px" Width="400px"></asp:ListBox> <br /> <br /> <br /> <asp:Button ID="btnDisplayAll" runat="server" Text="Display All" Font-Bold="true" OnClick="btnDisplayAll_Click" /> </center> </div> </form> </body> </html>
Step 3:-
Right click the application in Solution Explorer and select the Add New Item option from the context menu. This open the Add New Item dialog box.
Step 4:-
In this dialog box, select the LINQ and SQL Classes template and rename the file as MyDataClasses.dbml.
Step 5:-
Click the Add button, as shown in following image.
Now your application look like as:-
Step 6:-
Drag and drop the table from Server Explor. This action adds the table to your appplication, and you can retrieve any data from the table. As shown following image.
Connection String automatically created in your web.config file as shown in below:-
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="dbSantoshTestConnectionString" connectionString="Data Source=PS-PC\SANTOSH;Initial Catalog=dbSantoshTest;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> </assemblies> </compilation> </system.web> </configuration>
Step 7:-
Now go to Defaul.aspx design page and double click on Display All button and write the following code in your project:-
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; public partial class _Default : System.Web.UI.Page { string CS = ConfigurationManager.ConnectionStrings["dbSantoshTestConnectionString"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { } protected void btnDisplayAll_Click(object sender, EventArgs e) { MyDataClassesDataContext objDC = new MyDataClassesDataContext(CS); var query= objDC.tblLinqDemos; foreach (var record in query) { lbDetail.Items.Add(record.Id+" \t"+ record.Name.ToString() + " " + record.Branch.ToString() + " " + record.Address.ToString()+"\n\n"); } } }
Step 8:-
Now run the application and click on Display All button and get out put:-
No comments:
Post a Comment