作甚?
先说说这个工具是干啥的,我们所做的程序,或多或少需要存储一些数据到数据库,当然直接使用Sql语句也可以,甚至有些情况下就是要使用sql语句,但对于一些基本的增删改查,对每张表都要写查询语句就显得效率低下了,并且查询出来的数据没有映射成对象,我们自己还要去转换一次,显得麻烦。这个时候就有很多很多ORM框架出现了,按我浅薄的理解就是将数据库中的表映射成代码中对象,增删改查都用对象封装起来,用起来就很舒服,当然映射这些工作必须是自动完成的,能够根据数据库的表结构自动生成,今天介绍的SqlMetal.exe就是其中一种。
SqlMetal.exe
这个工具安装了vs就会有,它的路径在
c:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\下,很多,功能也很强大,粗糙的用法就是,先有了数据库,然后用这个工具去根据这个数据库生成相应的对象模型代码文件,我们把这些文件包含进我们的程序,就能够对数据库进行操纵了。sqlmetal /conn:"Server=np:\\.\pipe\LOCALDB#88239602\tsql\query;Database=Northwind;Integrated Security=true" /dbml:../Northwind.dbmlSqlMetal /code:../Northwind.cs /map:../Northwind.map ../Northwind.dbml
第一句命令,是告诉SqlMetal链接字符串/conn:
,他会根据这个数据库生成一个dbml文件/dbml:
增
我们发现数据库中有这张表Customers
,而程序中也有了一个Customers
对象,所以我们来增加一个客户信息
private static Northwind _db = new Northwind(@"Server=np:\\.\pipe\LOCALDB#0FA0957B\tsql\query;Database=Northwind;Integrated Security=true"); static void Main(string[] args) { var customer = new Customers() { Address = "KunMing", City = "YunNan", CompanyName = "999", ContactName = "Bob", ContactTitle = "Owner", Country = "China", Fax = "030-0076545", Phone = "030-0074321", PostalCode = "12209", CustomerID = "SIMOC", }; _db.Customers.InsertOnSubmit(customer); _db.SubmitChanges(); }
改
private static Northwind _db = new Northwind(@"Server=np:\\.\pipe\LOCALDB#0FA0957B\tsql\query;Database=Northwind;Integrated Security=true"); static void Main(string[] args) { var customer = _db.Customers.FirstOrDefault(x => x.CompanyName == "999"); if (customer != null) { customer.CompanyName = "888"; _db.SubmitChanges(); } }
删
private static Northwind _db = new Northwind(@"Server=np:\\.\pipe\LOCALDB#0FA0957B\tsql\query;Database=Northwind;Integrated Security=true"); static void Main(string[] args) { var customer = _db.Customers.FirstOrDefault(x => x.CompanyName == "888"); if (customer != null) { _db.Customers.DeleteOnSubmit(customer); _db.SubmitChanges(); } }
查
private static Northwind _db = new Northwind(@"Server=np:\\.\pipe\LOCALDB#0FA0957B\tsql\query;Database=Northwind;Integrated Security=true"); static void Main(string[] args) { var customer = _db.Customers.FirstOrDefault(x => x.CompanyName == "888"); }
视图和存储过程
这些都是简单的操作,给我们节省了很多时间,但LinqToSql用于多表联合查询时,特别是带统计的查询时,效率就会有所下降,为什么呢,一般我们在写linq语句的时候,如下面这种:
var customer = from c in _db.Customers where c.CompanyName == "888" select c;
它是会等待真正使用数据的时候才去查询,如.ToList
方法,Count
方法的时候,当我们使用LinqToSql的时候,一旦分组统计中使用了.Count
方法,那么它就会执行一次查询,试想,如果有很多行需要统计的数据,那么是不是要查询很多次,就网络中的延迟就会导致这个查询效率很低,解决的办法就是使用存储过程
or视图
。SqlMetal同样可以生成视图和存储过程,我们在数据库中添加简单的视图和存储过程
create view View_customeasselect * from dbo.Customersgocreate proc Proc_GetCustomerById( @Id nvarchar(100))asselect * from dbo.Customers where CustomerID=@Idgo
然后修改上面的命令为:
sqlmetal /conn:"Server=np:\\.\pipe\LOCALDB#0FA0957B\tsql\query;Database=Northwind;Integrated Security=true" /dbml:../Northwind.dbml /views /functions /sprocs
重新生成
var viewResult = _db.View_custome.ToList();var procResult = _db.Proc_GetCustomerById("ALFKI").FirstOrDefault();
视图和存储过程都能使用啦,是不是觉得很方便。
如果有错误的地方还请指正。