Add, modify, delete, and query operations for ASP. NET LINQ Connection database SQL execution data

Source: Internet
Author: User
Tags connectionstrings

Querying data in a database

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.SqlClient; Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Dataclassesdatacontext lqdb=NewDataclassesdatacontext (configurationmanager.connectionstrings["db2016connectionstring"].        Connectionstring.tostring ()); varresult = fromRinchlqdb.newswhereR.id >0                     SelectR; Gridview1.datasource=result;     Gridview1.databind (); }}

Adding data to the database

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Configuration; Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {    }    protected voidButton1_Click (Objectsender, EventArgs e) {Linqdbdatacontext lqdb=NewLinqdbdatacontext (configurationmanager.connectionstrings["db2016connectionstring"].        Connectionstring.tostring ()); News Info=NewNews (); Info.content=TextBox1.Text;        LqDB.news.InsertOnSubmit (info);        Lqdb.submitchanges (); Page.ClientScript.RegisterStartupScript (GetType (),"","alert (' Publish Success '); Location.href= ' Default.aspx '",true); }}

modifying data

  protected voidPage_Load (Objectsender, EventArgs e) {Linqdbdatacontext lqdb=NewLinqdbdatacontext (configurationmanager.connectionstrings["db2016connectionstring"].        Connectionstring.tostring ()); varresult = fromRinchlqdb.newswhereR.id = =1                     SelectR; foreach(News Info1inchresult) {info1.content="There's nothing I can't do, I'm the best";    } lqdb.submitchanges (); }

Delete data

New Linqdbdatacontext (configurationmanager.connectionstrings["db2016connectionstring" ]. Connectionstring.tostring ());         var  from inch lqdb.news                      where 1                     Select R;        LqDB.news.DeleteAllOnSubmit (result);        Lqdb.submitchanges ();

Add, modify, delete, and query operations for ASP. NET LINQ Connection database SQL execution data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.