本教程旨在帮助初学者快速掌握使用LINQ进行数据库的CRUD(创建、读取、更新、删除)操作。将从内存操作与物理提交的区别开始,逐步深入到每个数据库操作的具体代码实现。
LINQ技术的核心是实体对象。当数据提交到数据库时,它会首先通过LINQ对象。数据库操作是通过'DataContext'类进行的。由于实体对象是LINQ的基础,所有数据首先发送到这些实体,然后才路由到实际的物理数据库。因此,数据库提交是一个两步过程:第一步是内存操作,最后一步是物理提交。
首先,需要创建一个实体类。以一个客户表为例,包含客户ID、客户代码和客户名称字段。以下是创建实体类的代码示例:
using System;
using System.Data.Linq.Mapping;
[Table(Name = "Customer")]
public class CustomerEntity
{
private int _CustomerId;
private string _CustomerCode;
private string _CustomerName;
[Column(DbType = "nvarchar(50)")]
public string CustomerCode
{
set { _CustomerCode = value; }
get { return _CustomerCode; }
}
[Column(DbType = "nvarchar(50)")]
public string CustomerName
{
set { _CustomerName = value; }
get { return _CustomerName; }
}
[Column(DbType = "int", IsPrimaryKey = true, IsDbGenerated = true)]
public int CustomerId
{
set { _CustomerId = value; }
get { return _CustomerId; }
}
}
创建数据上下文:
DataContext objContext = new DataContext(strConnectionString);
设置插入数据:
CustomerEntity objCustomerData = new CustomerEntity();
objCustomerData.CustomerCode = txtCustomerCode.Text;
objCustomerData.CustomerName = txtCustomerName.Text;
进行内存更新:
objContext.GetTable<CustomerEntity>().InsertOnSubmit(objCustomerData);
执行物理提交:
objContext.SubmitChanges();
完整的创建LINQ代码:
DataContext objContext = new DataContext(strConnectionString);
CustomerEntity objCustomerData = new CustomerEntity();
objCustomerData.CustomerCode = txtCustomerCode.Text;
objCustomerData.CustomerName = txtCustomerName.Text;
objContext.GetTable<CustomerEntity>().InsertOnSubmit(objCustomerData);
objContext.SubmitChanges();
创建数据上下文:
DataContext objContext = new DataContext(strConnectionString);
选择要更新的LINQ对象:
var MyQuery = from objCustomer in objContext.GetTable<CustomerEntity>()
where objCustomer.CustomerId == Convert.ToInt16(txtCustomerId.Text)
select objCustomer;
设置新值并更新物理数据库:
CustomerEntity objCustomerData = (CustomerEntity)MyQuery.First<CustomerEntity>();
objCustomerData.CustomerCode = txtCustomerCode.Text;
objCustomerData.CustomerName = txtCustomerName.Text;
objContext.SubmitChanges();
完整的更新LINQ代码:
DataContext objContext = new DataContext(strConnectionString);
var MyQuery = from objCustomer in objContext.GetTable<CustomerEntity>()
where objCustomer.CustomerId == Convert.ToInt16(txtCustomerId.Text)
select objCustomer;
CustomerEntity objCustomerData = (CustomerEntity)MyQuery.First<CustomerEntity>();
objCustomerData.CustomerCode = txtCustomerCode.Text;
objCustomerData.CustomerName = txtCustomerName.Text;
objContext.SubmitChanges();
删除操作不需要创建数据上下文和选择LINQ对象,直接调用'DeleteOnSubmit()'方法进行内存删除,然后调用'SubmitChanges()'方法从数据库中删除。
objContext.GetTable<CustomerEntity>().DeleteOnSubmit(objCustomerData);
objContext.SubmitChanges();
查询并读取LINQ对象的值,以下代码示例展示了如何执行LINQ查询并将对象值设置到ASP.NET UI中。
DataContext objContext = new DataContext(strConnectionString);
var MyQuery = from objCustomer in objContext.GetTable<CustomerEntity>()
where objCustomer.CustomerId == Convert.ToInt16(txtCustomerId.Text)
select objCustomer;
CustomerEntity objCustomerData = (CustomerEntity)MyQuery.First<CustomerEntity>();
txtCustomerCode.Text = objCustomerData.CustomerCode;
txtCustomerName.Text = objCustomerData.CustomerName;