如何在C#中通过代码将Linq转换为SQL(通过代码连接至sql) [英] how to Linq to SQL by code in C# (connect to sql by code)

查看:649
本文介绍了如何在C#中通过代码将Linq转换为SQL(通过代码连接至sql)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Good Day
我已经使用ADO.net在SQL Server中连接和管理数据,现在我知道Linq了,我想使用它,但是我更喜欢仅通过代码来做所有事情,因此,如果您愿意,我想在LINQ方法:

Good Day
i have used ADO.net to connect and manage data in SQL server and now i knew about Linq and i want to use it but i prefer to do everything by code only,so if you please i want to write the following simple code in the LINQ method:

SqlConnection oConn = new SqlConnection("Data Source = roger\\sqlexpress;Initial Catalog = test; Integrated security=SSPI");
SqlCommand oCmd = new SqlCommand();
SqlDataReader Rdr;
            
oConn.Open();
            
oCmd.Connection = oConn;
oCmd.CommandType = CommandType.Text;
oCmd.CommandText = "select * from person";
            
Rdr = oCmd.ExecuteReader();
while(Rdr.Read())
    MessageBox.Show(Rdr["pin"].ToString());            
oConn.Close();



而且我想知道使用ADO和LINQ之间的区别.
谢谢



and i want to know the difference between using ADO and LINQ.
Thanks

推荐答案

在ADO.NET中,您可以自己管理连接和CRUD.
但是在LINQ中,它为数据库中的所有对象(简称DataContext)创建类.
您需要专注于处理数据,而不是如何做.

参见 http://blogs.msdn.com/b/wriju/archive/2008/07/14/linq-to-sql-vs-ado-net-a-comparison.aspx [
In ADO.NET you manage connection and CRUD by yourself.
But in LINQ, it creates classes for all objects in Database, in short DataContext.
You need to focus on what to do with Data, rather than how to do.

See http://blogs.msdn.com/b/wriju/archive/2008/07/14/linq-to-sql-vs-ado-net-a-comparison.aspx[^] for detail.


这篇关于如何在C#中通过代码将Linq转换为SQL(通过代码连接至sql)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆