
译文出处:http://www.codeproject.com/Tips/871938/LINQ-To-SQL-Using-Csharp
今天在这个话题中,我给大家分享一个在c#编程中非常有趣和十分有用的特性。
开始之前,我想告诉大家关于Linq的基本信息。比如:什么是linq?然后再来分享实际应用。
说明:
LINQ = Language Integrated Query(集成查询语言)
Linq是微软在.NET Framework 3.5中信增加的一个特性。它是用来查询数据库和对数据库查询的本地集合带来安全性。它非常简单但是很有组织性。一个普通的查询语言,适用于SQL, XML, 本地collections 和 第三方APIs 比如SharePoint.
本质上,Linq提供的就是一个轻量级的编程数据集成。这是非常有价值的,尤其是当今每天面对的数据和未来的大数据。
接下来我们就看看这个神秘的东东。
第一步:
- 打开你的Visual Studio 并且创建一个新的控制台项目.
- 然后打开服务器资源管理,创建一个新的数据库,新增一张表增加几个字段。
- 打开你的项目的解决方案目录,右击工程点击添加新增项。
- 查找LINQ-To-SQL 并添加。
- 你会看到一个空白的文件,在这个文件中你可以拖动你的表放在 LINQ-To-SQL .dbml 文件扩展上.
第二步:
我将声明一些类,添加一些类成员。
class Program
{ // this is program class
private int id;
private string name;
private string fname;
private int age;
private string sem;
}
第三步:
这里我将告诉大家如何通过 LINQ-To-SQL 向数据库中插入数据。
public void insert()
{
// these are the class data members through we will send our objects data in the database
Console.WriteLine("Enter id");
id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter name");
name = Console.ReadLine();
Console.WriteLine("Enter father name");
fname = Console.ReadLine();
Console.WriteLine("Enter age");
age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter semester");
sem = Console.ReadLine();
// 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.
LTSDataContext LTS = new LTSDataContext
(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
// 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.Father_Name = fname;
objStudentTable.Age = age;
objStudentTable.Semester = sem;
LTS.Students.InsertOnSubmit(objStudentTable); // this is built in function.
LTS.SubmitChanges();// here is the final query will run in the data context class.
}
第四步:展示数据
void Display()
{
LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
var selectQuery = from s in LTS.Students
select s;
foreach (Student s in selectQuery)
{
Console.WriteLine(s.Id + "\t" + s.Name + "\t" +
s.Father_Name + "\t" + s.Age + "\t" + s.Semester);
}
}
第五步:删除数据
void Delete()
{
int iid = ;
Console.WriteLine("Enter the Id of the student u want to delete?");
iid = Convert.ToInt32(Console.ReadLine()); LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
var delete = from p in LTS.Students
where p.Id == iid
select p;
LTS.Students.DeleteAllOnSubmit(delete);
LTS.SubmitChanges();
Student objStudentTable = LTS.Students.Single(c=> c.Id == iid);
}
第六步:更新数据
void update()
{
LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
Student objStudentTable = new Student();
int iid = ;
Console.WriteLine("Enter the Id of the student u want to update ?");
iid = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the new name of the student u want to update?");
string up = (Console.ReadLine());
var update = from s1 in LTS.Students
where s1.Id == iid
select s1;
foreach (var v in update)
v.Name = up;
LTS.SubmitChanges();
}
主函数:
static void Main(string[] arg){ Program p1 = new Program(); // creates object
p1.insert();
p1.Display();
p1.Delete();
p1.update();
Console.ReadKey();
}
感谢您的阅读,请留下您的足迹。
Cheers! Enjoy coding.