初识OR Mapping
公司数据库模块的操作
1.登入sqlserver连接到服务器,创建你需要的数据库
2.进入“\\server\project”,建立一个属于你自己的文件夹(方便让UI访问)
3.进入“http://server/mango/”,单击你自己的数据库,选择你要生成的路径,单击生成按钮
4.打开VS,创建一个类库,选中项目,右击,选择“属性”,在弹出的窗体中,选择“生成”,在输出路径中填入你要生成到的位置(应生成到服务器上。选中XML文档文件按钮)
5.添加引用,把原先数据库生成的DLL文件引用进来
好,现在我们就可以编写类了。以下是简单的增删改查实例,希望可以给公司新进来的学数据库的同事有所帮助
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test.Data;
using Test.Entities;
using System.Data;
namespace Tt{
/// <summary>
/// 订单类
/// </summary>
public class OrderService
{
/// <summary>
/// 添加一个客户信息
/// </summary>
/// <param name="name">客户名</param>
public void AddCus(string name)
{
Customer cus = new Customer();
cus.FName = name;
DataRepository.CustomerProvider.Insert(cus);
}
//直接传入对象直接使用即可
/// <summary>
/// 根据Id查询
/// </summary>
/// <param name="customerId"></param>
/// <returns></returns>
public Customer Find(long customerId)
{
return DataRepository.CustomerProvider.GetByFCustomerID(customerId);
}
/// <summary>
/// 根据name查询客户信息
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public TList<Customer> Find(string name)
{
CustomerQuery cu = new CustomerQuery();
cu.Append(CustomerColumn.FName, name);
return DataRepository.CustomerProvider.Find(cu);
}
/// <summary>
///根据name删除
/// </summary>
/// <param name="name"></param>
public void del(string name)
{
CustomerQuery cu = new CustomerQuery();
cu.Append(CustomerColumn.FName, name);
TList<Customer> t = DataRepository.CustomerProvider.Find(cu);
DataRepository.CustomerProvider.Update(t);
}
/// <summary>
/// 把第一个名为大王的字段改成name
/// </summary>
/// <param name="name"></param>
public void Set(string name)
{
CustomerQuery customerQuery = new CustomerQuery();
customerQuery.Append(CustomerColumn.FName, "大王");
TList<Customer> newCustomer = DataRepository.CustomerProvider.Find(customerQuery);
////修改记录
newCustomer[0].FName = name;
DataRepository.CustomerProvider.Update(newCustomer[0]);
}
/// <summary>
/// 根据Id把FName的值更改为name
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
public void Set(long id,string name)
{
Customer newCustomer = DataRepository.CustomerProvider.GetByFCustomerID(id);
////修改记录
newCustomer.FName = name;
DataRepository.CustomerProvider.Update(newCustomer);
}
/// <summary>
/// 深度更新
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
public void Set(long id, string name)
{
Order order = new Order();
order.FCustomerId = 1;
order.FCount = 2;
Customer newCustomer = DataRepository.CustomerProvider.GetByFCustomerID(id);
TList<OrderDetail> tlistOrderDetail = new TList<OrderDetail>();
//第一个订单详细
OrderDetail orderdetail = new OrderDetail();
orderdetail.FName = "相机";
orderdetail.ForderId = order.FOrdersId;
tlistOrderDetail.Add(orderdetail);
//第二个订单详细
OrderDetail orderdetail1 = new OrderDetail();
orderdetail1.FName = "电脑";
tlistOrderDetail.Add(orderdetail1);
orderdetail1.FOrderDetailId = order.FOrdersId;
//OrderDetailCollection订单详细集合
order.OrderDetailCollection = tlistOrderDetail;
DataRepository.OrderProvider.DeepSave(order);
}
}
}
//添加代码
//Teacher teacher = new Teacher();
//DataRepository.TeacherProvider.DeepLoad(teacher);
//teacher.FTeacherName = "蕾钧";
//DataRepository.TeacherProvider.Insert(teacher);
////修改代码
//TeacherQuery teacherQuery = new TeacherQuery();
//teacherQuery.Append(TeacherColumn.FTeacherName, "吕蕾钧");
//TList<Teacher> tListTeacherColumn = new TList<Teacher>();
//tListTeacherColumn = DataRepository.TeacherProvider.Find(teacherQuery);//找出表中FTeacherName字段中所有叫吕蕾钧的记录
////int n=tListTeacherColumn.Count;
////tListTeacherColumn[0].FTeacherName = "吕钧";
//DataRepository.TeacherProvider.Update(tListTeacherColumn[0]);
//深度添加
//Teacher teacher = new Teacher();
//teacher.FTeacherName = "测试5";
//teacher.FTeacherNumber = "4";
//TList<Course> tListCourse = new TList<Course>();
//Course course = new Course();
//course.FTeacherID = teacher.TTeacherID;
//course.FCourseName = "441212";
//tListCourse.Add(course);
//Course course1 = new Course();
//course1.FTeacherID = teacher.TTeacherID;
//course1.FCourseName = "C++3";
//tListCourse.Add(course1);
//teacher.CourseCollection = tListCourse;
//DataRepository.TeacherProvider.DeepSave(teacher);
//多条件查询
//TeacherQuery teacherQuery = new TeacherQuery();
//teacherQuery.Append(TeacherColumn.FTeacherName, "蕾钧");
//teacherQuery.BeginGroup("AND");
//teacherQuery.Append("", TeacherColumn.FTeacherNumber, "5", false);
//teacherQuery.BeginGroup("OR");
//teacherQuery.Append("", TeacherColumn.FTeacherNumber, "4", false);
//teacherQuery.EndGroup();
//teacherQuery.EndGroup();
////teacherQuery.Append(") AND ", "1", "1", false);
////teacherQuery.Append(TeacherColumn.FTeacherName, "4");
//TList<Teacher> tListTeacher = new TList<Teacher>();
//tListTeacher = DataRepository.TeacherProvider.Find(teacherQuery);
CustomerID LIKE @Param0 AND City = @Param1) OR (CustomerID LIKE @Param2 AND City = @Param3)
代码如下
CustomersParameterQuery query1 = new CustomersParameterQuery();
query1.Clear();
query1.Junction = string.Empty; // This prevents the ParameterBuilder from throwing an "AND" before next line's output
query1.BeginGroup();
query1.Append(string.Empty, CustomersColumn.CustomerID, "A%", true);
query1.Append("AND", CustomersColumn.City, "London", true);
query1.EndGroup();
query1.BeginGroup("OR");
query1.Append(string.Empty, CustomersColumn.CustomerID, "B%", true);
query1.Append("AND", CustomersColumn.City, "Berlin", true);
query1.EndGroup();
TList<Customers> list1 = DataRepository.CustomersProvider.Find(query1.GetParameters());
Console.WriteLine("Query1 = {0}", query1);
Results Query1
![](http://www.cnblogs.com/images/cnblogs_com/xsmhero/20091027.jpg)