Click here to Skip to main content
11,706,477 members (33,316 online)
Click here to Skip to main content

Create Linq to SQL connection with select, insert, delete, update

, 6 Feb 2011 CPOL 23.6K 8
Rate this:
Please Sign up or sign in to vote.
Create Linq to SQL connection with select, insert, delete, update
* Create new website
* Add new item
→ Select LINQ TO SQL classes
Then, create one dataclasses.dbml file.
Tool→connect to database→ change data source
→ Select datasource (Microsoft sqlserver)
→ Select data provider .NET Framework (data provider for sqlserver)
→ Select servername, SQL authentication and database
→ Select dataclasses.dbml and we can choose the table and drag into the dataclasses.dbml file. Class for that Table will be created automatically.

Codes on default.aspx.cs at code behind page.

private void gridview()
{
   DataClassesDataContext dh = new DataClassesDataContext();
   var s = from data in dh.tests 
           orderby data.id descending 
           select new { data.id ,data.name, data.@class };
       
   GridView1.DataSource = s;
   GridView1.DataBind();
}

protected void insert_Click(object sender, EventArgs e)
{
   DataClassesDataContext dh = new DataClassesDataContext();
   test t = new test { id = Guid .NewGuid ().ToString (), 
                       name =TextBox1 .Text , 
                       @class = TextBox2 .Text 
                      };
   dh.tests.InsertOnSubmit(t);
   dh.SubmitChanges();
   gridview();
}

protected void update_Click(object sender, EventArgs e)
{
   DataClassesDataContext dh = new DataClassesDataContext();
   //test tt = dh.tests.First(p => p.name.StartsWith("manu"));
   test tt=dh.tests.Single (p1 => p1.name.StartsWith ("manu"));
   tt.@class +=4;
   tt.name = "raju";
   dh.SubmitChanges();
   gridview();
}

protected void delete_Click(object sender, EventArgs e)
{
   DataClassesDataContext dh = new DataClassesDataContext();
   test tt = dh.tests.First(p => p.name.StartsWith("new"));
   dh.tests.DeleteOnSubmit(tt);
   dh.SubmitChanges();
   gridview();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SuhasHaridas
Software Developer Standout IT Solutions(P) Ltd.
India India
My name is SuhasHaridas.

I'm a Software Engineer in the .NET Developer Platform group within the Developer Division at Standout IT Solutions(P) Ltd.
I have focused on the Web platform during these years.Specifically, that means I have worked on ASP.NET and c#.NET ,SharePoint,Sql, WCS Apps(Cisco Wireless Control System),.NET Compact Framework, LINQ , ADO.NET , Ado.net entity framework ,Silverlight , WCF Services , SQL SQLCE Visual Studio and Visual Studio Web Developer as well as on IIS7.

You may also be interested in...

Comments and Discussions

 
Questioninsert using linq Pin
Member 995422424-Apr-14 5:43
memberMember 995422424-Apr-14 5:43 
Questionlinq to sql basic Pin
SuhasHaridas13-Aug-12 0:15
memberSuhasHaridas13-Aug-12 0:15 
GeneralWhat is most advantage of Linq to sql? Pin
kishore30914-Feb-11 19:08
memberkishore30914-Feb-11 19:08 
GeneralRe: What is most advantage of Linq to sql? Pin
SuhasHaridas13-Aug-12 0:19
memberSuhasHaridas13-Aug-12 0:19 
GeneralReason for my vote of 3 nothing excited!! Pin
maq_rohit8-Feb-11 1:28
membermaq_rohit8-Feb-11 1:28 
Generaledited for formatting and spelling Pin
Shahriar Iqbal Chowdhury5-Feb-11 4:47
memberShahriar Iqbal Chowdhury5-Feb-11 4:47 
GeneralThank you for your tip. Besides the formatting also include ... Pin
Patrick Kalkman5-Feb-11 2:07
memberPatrick Kalkman5-Feb-11 2:07 
GeneralRe: Thank you for your tip. Besides the formatting also include ... Pin
SuhasHaridas13-Aug-12 0:20
memberSuhasHaridas13-Aug-12 0:20 
GeneralPlease fix the formatting Pin
Indivara5-Feb-11 1:15
subeditorIndivara5-Feb-11 1:15 
GeneralRe: Please fix the formatting Pin
SuhasHaridas13-Aug-12 0:20
memberSuhasHaridas13-Aug-12 0:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by SuhasHaridas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid