栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

LINQ To SQL

LINQ To SQL

LINQ = Language Integrated Query(集成查询语言)

Linq是微软在.NET framework 3.5中信增加的一个特性。它是用来查询数据库和对数据库查询的本地集合带来安全性。它非常简单但是很有组织性。一个普通的查询语言,适用于SQL, XML, 本地collections 和 第三方APIs 比如SharePoint.

本质上,Linq提供的就是一个轻量级的编程数据集成。这是非常有价值的,尤其是当今每天面对的数据和未来的大数据。

接下来我们就看看这个神秘的东东。

第一步:

打开你的Visual Studio 并且创建一个新的控制台项目.
然后打开服务器资源管理,创建一个新的数据库,新增一张Studuent表增加几个字段。
打开你的项目的解决方案目录,右击工程点击添加新增项。
查找LINQ-To-SQL 并添加,
你会看到一个空白的DataClasses1.dbml 文件,在这个文件中你可以拖动你的表放在 DataClasses1.dbml 文件扩展上.

第二步:
如何通过 LINQ-To-SQL 向数据库中增删改查。

/// 
/// 插入数据
/// 
public void insert()
{ 
    
    // these are the class data members through we will send our objects data in the database
    int id = 1;

    string name = "2";

    string fname = "2";

    int age = 3;

    string sem = "4";
    // this is the data context class the main class which handles
    // all the functionality in this will pass the connection string of our database file.

    DataClasses1DataContext db = new DataClasses1DataContext("data source=192.168.1.150;initial catalog=bageDB;persist security info=True;user id=tcps;password=t123456");
    // this is the table class which we drag on our linq to sql file
    Student objStudentTable = new Student();
    objStudentTable.id = id;
    objStudentTable.name = name;
    objStudentTable.fname = fname;
    objStudentTable.age = age;
    objStudentTable.sem = sem;
    db.Student.InsertOnSubmit(objStudentTable); // this is built in function.
    db.SubmitChanges();// here is the final query will run in the data context class.
}

/// 
/// 查询数据
/// 
void Select()
{
    DataClasses1DataContext db = new DataClasses1DataContext("data source=192.168.1.150;initial catalog=bageDB;persist security info=True;user id=tcps;password=t123456");
    var selectQuery = from s in db.Student
        select s;
    foreach (Student s in selectQuery)
    {

    }
}

/// 
/// 删除数据
/// 
void Delete()
{
    int id = 1;

    DataClasses1DataContext db = new DataClasses1DataContext("data source=192.168.1.150;initial catalog=bageDB;persist security info=True;user id=tcps;password=t123456");

    var delete = from p in db.Student
        where p.id == id
        select p;
    db.Student.DeleteAllOnSubmit(delete);
    db.SubmitChanges();
    Student objStudentTable = db.Student.Single(c => c.id == id);
}

/// 
/// 更新数据
/// 
void update()
{
    DataClasses1DataContext db = new DataClasses1DataContext("data source=192.168.1.150;initial catalog=bageDB;persist security info=True;user id=tcps;password=t123456");
    
    Student objStudentTable = new Student();

    int id = 1;

    var update = from s1 in db.Student
        where s1.id == id
        select s1;
    foreach (var v in update)
        v.name = "bage";
    db.SubmitChanges();
}

总结:

LINQ To SQL 与 LINQ To EF 异同点

LINQ To SQL 与 LINQ To EF 语法差异也比较明显。
LINQ to SQL使用的命名空间是System.Data.Linq。
LINQ to EF使用的命名空间是System.Data.Entity。

LINQ To SQL 与 LINQ To EF 相同点

两个都需要引用System.Linq类库。LINQ查询语法一样,要使用from、where、orderby等关键字。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/327323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号